如何使用 linq 按某些字段选择整个顶行组
本文关键字:选择 字段 linq 何使用 | 更新日期: 2023-09-27 18:34:24
我尝试从列表中选择一个项目,该项目对于归档的频率最高,因此我根据该归档对项目进行分组并按降序排序。现在我想选择顶行。
我试过了:
private Rule MatchRule(string cond)
{
var results = (from x in rules
where x.Cond == cond
group x by new { x.Tree.Val , r = x.Tree.Right.Val, l = x.Tree.Left.Val}
into g
select new {
rule = ???,
Count=g.Count(),
}).OrderByDescending(x => x.Count).ToList();
return results[0].rule;
}
我应该使用什么而不是???
来选择项目的所有字段(整个项目)(顶行)。
我找到了,我必须使用First()
来选择整行
private Rule MatchRule(string cond)
{
var results = (from x in rules
where x.Cond == cond
group x by new { x.Tree.Val , r = x.Tree.Right.Val, l = x.Tree.Left.Val}
into g
select new {
rule = g.First(),
Count=g.Count(),
}).OrderByDescending(x => x.Count).ToList();
return results[0].rule;
}