如何构建将保留 ThenBy 顺序的表达式?下面的表达式生成按 Bowler.Number 排序的组,但每个组的匹配项未排序。
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)
这是我想要的输出:
1
game1 295
game2 199
game3 202
game4 178
2
game1 177
...
目前我使用两个 foreach 之类的
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
}
}
没什么大不了的,我只是认为 LINQ 能够在构建组时帮助我对组中的匹配项进行排序,而不是稍后在我处理它们时。