NHibernate使用SqlFunction选择列,按相同标准分组

时间:2011-05-13 13:33:09

标签: nhibernate queryover

我需要根据每天查询计数以及按相同标准分组。生成的查询应与

类似
select SendTo, dateadd(dd,
                 0,
                 datediff(dd,
                          0,
                          WorkToBeginDate))
from Locates
group by SendTo, dateadd(dd,
                 0,
                 datediff(dd,
                          0,
                          WorkToBeginDate))

我目前正在使用下面的查询,但它没有按date分组。

var dateGroupBy = Projections.SqlFunction("date", NHibernateUtil.Date,
    Projections.Group<Domain.Locate>(g => g.WorkToBeginDate));

var stats = 
    _session.QueryOver<Domain.Locate>()
    .SelectList(x => x
        .SelectGroup(xx => xx.SendTo).WithAlias(() => statsDto.SentTo)
        .SelectCount(xx => xx.LocateId).WithAlias(() => statsDto.Count)
        .Select(dateGroupBy)
        .WithAlias(() => statsDto.DueDate))
    .TransformUsing(Transformers.AliasToBean<StatsDto>())
    .List<StatsDto>();

执行此查询会产生

SELECT   this_.SendTo                             as y0_,
         count(this_.LocateId)                    as y1_,
         dateadd(dd,
                 0,
                 datediff(dd,
                          0,
                          this_.WorkToBeginDate)) as y2_
FROM     Locates this_
GROUP BY this_.SendTo,
         this_.WorkToBeginDate

我假设是因为我在预测中使用Select而不是SelectGroup。我尝试了.SelectGroup(xx => new SqlFunctionProjection("date", NHibernateUtil.Date, Projections.Group<Domain.Locate>(g => g.WorkToBeginDate))),但这给了我Could not determine member from new SqlFunctionProjection("date", NHibernateUtil.Date, new [] {Group(g => Convert(g.WorkToBeginDate))})

1 个答案:

答案 0 :(得分:3)

您可以使用另一个Projections.Group方法嵌套dateGroupBy值。

var dateGroupBy = Projections.Group(Projections.SqlFunction("date", NHibernateUtil.Date,
Projections.Group<Domain.Locate>(g => g.WorkToBeginDate)));

这应该会给你想要的结果。 :)