LINQ使用Group with Count和Where,e​​asy SQL,在LINQ中更难

时间:2013-04-19 17:01:39

标签: performance linq entity-framework linq-to-sql

我正在尝试显示计数大于1的城市名称。我可以在SQL中轻松完成,并且在LINQ中很接近,但无法弄清楚如何使用组并获取计数并显示名称

        var query = (from c in Consumer
                   group c
                   by new { c.City, size = c.City.Count() }
                       into results
                       select new { Name = results.Key.City })
                    .Where(a => size > 0);

尺寸部分不起作用

1 个答案:

答案 0 :(得分:4)

尝试此查询:

var list= Consumer.GroupBy(s=>s.City)
              .Select(s=>new {
                          City = s.Key,
                          size = s.Count(),
                   })
              .Where(s=>s.size>0).ToList();