2016-02-12 28 views
0

我嘗試從一個列表中選擇一個項目的頻率最高的項目,所以我根據該項目對項目進行分組,並對它們進行降序排序。現在我想選擇最上面一行。如何使用linq選擇整個頂行組的一些字段

我想:

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; 
    } 

我應該用什麼來代替???選擇項目(整個項目)(前行)的各個領域。

+0

你的意思是「所有字段」,你的意思是每個屬性? – krillgar

+0

請舉個例子。 – xoxox

回答

0

我找到了,我必須用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; 
} 
相關問題