我正在尝试使用NHibernate Linq
基于数据库字段的值进行条件分组。这个结构:
var result = Session.Query<Nalog>()
.GroupBy(x => new {x.Radionica.Naziv, x.Radionica.Sifra})
.Select(x => new RadioniceReportDto
{
Naziv = x.Key.Naziv,
Sifra = x.Key.Sifra,
Zakljuceno = x.Where(i => i.VrstaZakljucka == null).Count(),
Ukupno = x.Count()
}).ToList();
产生这个sql输出:
select
radionica1_.Naziv as col_0_0_,
radionica1_.Sifra as col_1_0_,
radionica1_.Naziv as col_2_0_,
radionica1_.Sifra as col_3_0_,
cast(cast(count(*) as INT) as DECIMAL(19, 5)) as col_4_0_,
cast(cast(count(*) as INT) as DECIMAL(19, 5)) as col_5_0_
from
Nalog nalog0_ left outer join
Radionica radionica1_ on nalog0_.RadionicaId=radionica1_.Id
group by
radionica1_.Naziv ,
radionica1_.Sifra
如您所见,首先在分组中重复两次sifra
和naziv
字段。此外,分组中的where
条件对sql输出没有影响。
更改行:
Zakljuceno = x.Where(i => i.VrstaZakljucka == null).Count()
为:
Zakljuceno = x.Count(i => i.DatumZakljucka == null)
对生成的sql也没有影响。我还下载了最新的NHibernate二进制文件,但问题仍然存在。