I need to sort a list by numer od appearances and show repeating items only once. For example I have a following list : "a", "b", "a", "a", "c", "d", "c". I want it to be sorted like this: "a","c","b",d". How can I achieve this?
That is what I made:
var something = from c in db.Letters
group c by c.letter into p
orderby p.Count()
select new
{
p.letter
};
But expression p.letter cannot be used.
var resunt = from c in db.Letters
group c by c.letter into p
orderby p.Count() descending
select p.Key;
When you use GroupBy you get an IGroupping object that contains a property of the Key object that you've grouped by and a collection of the values in the group. In your case you want to return the Key - the c.letter
Related
I am having some problem for a custom sorting required for one of row groups that I have in a SSRS table.
The logic for the custom sort order -
If the row group value contains a particular value then it should always be displayed at the bottom and all the other values have to be displayed in the ascending order.
For e.g. -
Suppose from the list of values A,E,G,D,C,and F, "D" should be always be displayed last and the other elements are to be sorted in asc order.
So, the above list should be sorted in the following order -
A,B,C,E,F,G,D
Or if the list of elements is - P,J,M,N,D,C,K
the required sort order is -
C,J,K,M,N,P and D.
This logic has to be implemented for the row group data which gets displayed in the report.
Would appreciate if someone can help me out on this.
Thank you.
Try using the following expression in the Sorting setting.
=IIF(
Fields!YourField.Value="D","ZZZZ" & Fields!YourField.Value,
Fields!YourField.Value
)
This will sort your group if you don't have groups whose four first letters are ZZZZ.
Let me know if this helps.
I use an IIF (or multiple IIFs) to do custom sorts like this.
For your situation:
A,E,G,D,C,and F, D should be always be displayed last and the other
elements are to be sorted in asc
I would first do a custom sort:
=IIF(Fields!MyFIeld.Value = "D", 2, 1)
This would sort the D first.
Then add second Sort that just uses the field (Myfield) to sort the rest by the field.
For the second situation:
if the list of elements is - P,J,M,N,D,C,K the required sort order is C,J,K,M,N,P and D
Then I would make a single custom sort with multiple IIFs:
=IIF(Fields!MyFIeld.Value = "C", 1,
IIF(Fields!MyFIeld.Value = "J", 2,
IIF(Fields!MyFIeld.Value = "K", 3,
IIF(Fields!MyFIeld.Value = "M", 4,
IIF(Fields!MyFIeld.Value = "N", 5,
IIF(Fields!MyFIeld.Value = "P", 6,
IIF(Fields!MyFIeld.Value = "D", 7, 8)))))))
-
I created a tablix with totals and I was able to sort by alphabetic order, total(ascending), total(descending). First I create a Dataset like this:
Select 'Name' as Order_Col, 1 as Order_Num
union
Select 'Ascending' as Order_Col, 2 as Order_Num
union
Select 'Descending' as Order_Col, 3 as Order_Num
order by 2
Then in the column group section, group properties I insert the following expression in the sorting options:
=Switch(Parameters!SortOrder.Value = 1,Fields!Name.Value
,Parameters!SortOrder.Value = 3,(Fields!TtlRef.Value)*-1
,Parameters!SortOrder.Value = 2,Fields!TtlRef.Value)
Create a Parameter named SortOrder where the Value is Order_Num and Label is Order_Col.
You can make a default using a value of 1.
I need to generate a string that has a comma delimited list, but no comma after the last element.
var x = new List<string>() { "a", "b", "c" };
should yield:
a,b,c
Yes, a very simple thing to do using "normal" techniques, but I hope with linq there is a more elegant way.
var cols =context.Database.SqlQuery<String>("select Column_Name from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = {0};", prefix + table);
No need Linq, just use String.Join
String.Join(",", new List<string>() { "a", "b", "c" });
String class provide Join method to join string array with delimiter.
Code:
var x = new List<string>() { "a", "b", "c" };
String.Join(",",x.ToArray());
documentation: http://msdn.microsoft.com/en-us/library/57a79xd0(v=vs.110).aspx
Although I strongly recommend you use the answer of Siva Charan, just for information here's an implementation in LinQ using Enumerable.Aggregate Method (IEnumerable, Func):
var result = x.Aggregate((c, n) => c + "," + n);
I use LINQ on a Dictionary<string, IList<ID>> like this:
var searchCategories = new List {"A", "B", "C"};
Result = CategoryMapper.Mapping.Where(
x => searchCategories.Contains(x.Key)).
Select(x => new Tuple<string, IList<ID>>(x.Key, x.Value)).ToList();
This returns all ids that are either in Category A, B or C. However what I would like to do is retrieve ids that are in Category A, B and C.
I'm having difficulties figuring out how to do this with Linq.
UPDATE
I'm sorry but I should have added some more information in my initial post. The lists in my dictionary look somewhat like this (I only use numbers here to make it simple):
A : {1, 2, 3}
B : {1,3}
C : {3}
So what I would like as a result of my query would be '3' in this case because it is the only number that has all categories.
Looks like you're just taking the intersection of all the lists. That should be simple to obtain.
var searchCategories = new HashSet<string> { "A", "B", "C" };
var result = CategoryMapper.Mapping
.Where(map => searchCategories.Contains(map.Key))
.Select(map => map.Value as IEnumerable<ID>)
.Aggregate((acc, cur) => acc.Intersect(cur));
If your ID type doesn't implement the IEquatable<ID> interface, then you may need to provide an equality comparer (that I assume you have) to perform the comparisons.
....Aggregate((acc, cur) => acc.Intersect(cur, new YourIdEqualityComparer()));
You can try to change x => searchCategories.Contains(x.Key) to x => searchCategories.All(c => x.Key.Contains(c)), i.e. the final code snippet should be
var searchCategories = new List<string> {"A", "B", "C"};
Result = CategoryMapper.Mapping.Where(
x => searchCategories.All(c => x.Key.Contains(c))).
Select(x => new Tuple<string, IList<ID>>(x.Key, x.Value)).ToList();
To get all the ids, the SelectMany method is perfect for this:
var ids = CategoryMapper.Mapping.SelectMany(kv => kv.Value);
i want to get an array of id's which is like ["15", "26", "37", "48", "90"] and i want to get my remaining items from my remaining table that doesnt includes these supplier id's..
here what i done so far:
string[] arrgroupdetails;
arrgroupdetails = dataContext.GroupDetails.Select(c => c.supplier_id).ToArray();
var items = from thingies in dataContext.remainings where thingies.supplier_id.ToString() != arrgroupdetails.Any().ToString() select thingies;
so how can i achive this?
By heart, so do check syntax but someething like this should work:
var items = from thingies in dataContext.remainings
where !arrgroupdetails.Contains(thingies.supplier_id.ToString())
select thingies;
how do i join the results in a IEnumerable to a single string?
the IEnumerable contains 20 single letters, and i want it to combine it to a single string.
And out of curiousity: how would i join it with a separator, for example if the IEnumerable contains the strings a b c d e how can i join it to a,b,c,d,e?
Michel
Try this:
IEnumerable<string> letters = new[] { "a", "b", "c", "d", "e" };
string separator = ", ";
string withSeparator = String.Join(separator, letters.ToArray());
string withoutSeparator = String.Join(String.Empty, letters.ToArray());
Also, with 4.0 .NET there's a new simpler overload available: String.Join Method (String, IEnumerable<String>) so you can skip the ToArray() call.