如何使用NHibernate Linq进行条件“分组依据”查询

时间:2013-04-22 07:30:03

标签: nhibernate

我正在尝试使用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

如您所见,首先在分组中重复两次sifranaziv字段。此外,分组中的where条件对sql输出没有影响。

更改行:

Zakljuceno = x.Where(i => i.VrstaZakljucka == null).Count()

为:

Zakljuceno = x.Count(i => i.DatumZakljucka == null)

对生成的sql也没有影响。我还下载了最新的NHibernate二进制文件,但问题仍然存在。

0 个答案:

没有答案