I have the following linq query, which works fine. I'm not sure how i order the group'd result.
from a in Audits join u in Users on a.UserId equals u.UserId group a by a.UserId into g select new { UserId = g.Key, Score = g.Sum(x => x.Score) }
the results are currently ordered by UserId ascending. I'm after Score descending.
thanks :)
GroupBy(student => student.Name) . Select(group => new { Name = group. Key, Students = group. OrderByDescending(x => x.
Found answer on MSDN: Yes.
A LINQ query can end with a GroupBy or Select clause. The result of GroupBy operators is a collection of groups. For example, GroupBy returns IEnumerable<IGrouping<TKey,Student>> from the Student collection: Return type of GroupBy()
GroupBy allows you to quickly group collections of related data by specific properties on your data. The grouped data is then arranged by sub-collections of items in those groups. Note: LINQ provides variants of each method in this article that work with either IEnumerable or IQueryable .
Just add the orderby clause ;-)
from a in Audits join u in Users on a.UserId equals u.UserId group a by a.UserId into g let score = g.Sum(x => x.Score) orderby score descending select new { UserId = g.Key, Score = score };
var results = (from a in Audits join u in Users on a.UserId equals u.UserId group a by a.UserId into g select new { UserId = g.Key, Score = g.Sum(x => x.Score) }) .OrderByDescending(p=>p.Score);
Hope this will fix your problem, easier than the top one ;)
Cheers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With