分组 EF 查询中的查询行数
Query Row count from Grouped EF query
我有这个查询来对 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 表达式或其他东西,但我就是听不懂。
UPDATE 我正在寻找的是一种 NOT 来编写 4 个不同查询的方法。例如:
var level1 = awards.Level[0]
var level2 = awards.Level[1]
尝试:
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 );
我有这个查询来对 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 表达式或其他东西,但我就是听不懂。
UPDATE 我正在寻找的是一种 NOT 来编写 4 个不同查询的方法。例如:
var level1 = awards.Level[0]
var level2 = awards.Level[1]
尝试:
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 );