使用Linq to NHibernate(3.1)如何计算这些记录
我有一个这样定义的实体:使用Linq to NHibernate(3.1)如何计算这些记录,linq,nhibernate,Linq,Nhibernate,我有一个这样定义的实体: public class TestEntity : EntityBase { public string Source {get;set;} public bool Suppressed {get;set;} /* other stuff */ } 我想显示一个如下所示的HTML表: Source Suppressed Not Suppressed ------------------------------------
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),
}
这显然不是我想要的…像g1那样做怎么样。Count(x=>x.supprested==true)?刚才试过了。由于对象以任意方式返回,因此已抑制和未抑制的计数都相同。在SQL中,我通过执行我最初试图模仿的sum(case…end)语句来解决这个问题。如果忽略谓词条件,可能是NHibernate错误。
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