我有这个查询来分组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]
答案 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 );