查询Grouped EF查询的行数

时间:2015-02-23 02:57:01

标签: c# entity-framework lambda

我有这个查询来分组EF

中特定行的级别
var awards = from a in context.Awards
             where a.TWID == employee.TWID
             group a by a.AwardLevel;

这给了我每个级别的奖励(1-4)我试图弄清楚如何从特定级别的奖项中提取计数。

ie: level1.count,level2.count etc.

我知道这应该是一些简单的lambda表达式或其他东西,但我无法得到它。

更新我正在寻找的方法是来编写4个不同的查询。例如:

var level1 = awards.Level[0]
var level2 = awards.Level[1]

1 个答案:

答案 0 :(得分:3)

尝试:

var awards = from a in context.Awards
             where a.TWID == employee.TWID
             group a by a.AwardLevel into award
             select new
            {
               AwardLevel = award.Key,
               Count = award.Count()
            }; 

根据更新的问题更新:

var awards = (from a in context.Awards
                 where a.TWID == employee.TWID
                 group a by a.AwardLevel into award
                 select new
                {
                   AwardLevel = award.Key,
                   Count = award.Count()
                }).ToDictionary( t => t.AwardLevel, t => t.Count );