How do I build an expression that will preserve the ThenBy order?
The Expression below produces the Groups ordered by Bowler.Number but the Matches of each group are not sorted.
public class Match
{
public int Id { get; set; }
public virtual Bowler Bowler { get; set; }
public byte GameNumber { get; set; }
public int Score { get; set; }
...
}
public class Bowler
{
public int Id { get; set; }
public int Number { get; set;}
...
}
var GroupedMatches = db.Matches.OrderBy(m=>m.Bowler.Number).ThenBy(m=>m.GameNumber).GroupBy(m => m.Bowler.Number)
Here is the output I want:
1
game1 295
game2 199
game3 202
game4 178
2
game1 177
...
Current I use two foreach like
foreach (var item in TheGroups){
... do some stuff with the Group
foreach (var x in item.OrderBy(a =>a.Number)) { //<-- can I get rid of this OrderBy?
... do some stuff with the Matches in this group
}
}
No big deal, I just thought LINQ would be able to help me sort the matches in the group when the groups where built, not later when I process them.
I’m not sure what you want the output to be, since it’s kind of non-sensical to order a list of groups keyed on bowler number by game number.
Assuming you want an ordered list of bowler numbers, each containing an ordered list of games, something like this might work
Here’s specifically how you get the output you’re looking for