我有一个如此定义的实体:
public class TestEntity : EntityBase
{
public string Source {get;set;}
public bool Suppressed {get;set;}
/* other stuff */
}
我想显示一个看起来像的HTML表格:
Source Suppressed Not Suppressed
-------------------------------------------------
Source1 30 1225
Soure 7 573
我首次尝试查询此内容:
from e in _session.Query<TestEntity>()
group e by e.Source into g1
select new
{
Source = g1.Key,
Suppressed = g1.Sum(x=>x.Suppressed ? 1 : 0),
NotSuppressed = g1.Sum(x=>x.Suppressed ? 0 : 1),
}
但是当然,Linq在将其转换为SQL时会对三元表达式感到窒息。有没有其他方法可以做到这一点?
编辑:我尝试了德米特里的建议,并为两者返回相同的计数。他的建议生成的SQL是:
select
customer0_.SourceA as col_0_0_,
cast(count(*) as INT) as col_1_0_,
cast(count(*) as INT) as col_2_0_
from
dbo.Customers customer0_
group by
customer0_.SourceA
这显然不是我想要的......
答案 0 :(得分:3)
做g1.Count(x => x.Suppressed == true)
之类的事情呢?