2017-03-29 35 views
-3

我需要获得主题名称和次数(计数)一个给定的主题发生。Linq group by和得到发生次数

例子:

Name: John 
Topic: X, Y, Z => these are List<string> 

Name: Bob 
Topic: Y 

Name: Suzy 
Topic: Y, Z 

Should generate output: 
X: 1 
Y: 3 
Z: 2 

我已经试过,但它不返回正确的结果:

var result = from r in items 
         orderby r.Topic 
         group r by r.Topic 
       into grp 
         select new { key = grp.Key, cnt = grp.Count() }; 
+2

“它不会返回正确的结果” - 这是从来没有足够的信息。它有什么作用?你能提供一个[mcve]证明这一点吗? (我们甚至无法知道目前代码中的数据是什么样子。) –

+2

分组之前的排序要点是什么? –

+1

@EricLippert它会影响组内项目的顺序,因为'GroupBy'确保组中的项目与原始顺序中的项目具有相同的相对顺序。至少对于'IEnumerable'实现。当然,这段代码似乎并不关心组中项目的顺序,但它至少*有时是明智的。 – Servy

回答

0

您可以拉平与其他from所包含的集合:

var result = from r in lineItems 
      from t in r.Topic // List<string> 
      group t by t into grp 
      orderby grp.Key 
      select new { key = grp.Key, cnt = grp.Count() }; 
+0

主题已在列表中表单中。对不起 – BobSwanson

+0

@BobSwanson:编辑 –

+0

如果orderby位于继续查询中,查询效率更高。你明白为什么? –

0

我喜欢用SelectMany来做这件事(为了展平内藏品) 只是另一种方式来做到这一点

var result = items.SelectMany((w) => w.Topic) 
        .GroupBy((q) => q) 
        .Select((p) => new { Grp = p.Key, Cnt = p.Count() }) 
        .OrderBy((g) => g.Cnt);