NHibernate select column with SqlFunction, group by the same criteria

I need to request an invoice based on each day, as well as groups based on the same criteria. The generated query should look something like this:

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

I am currently using the query below, but it is not grouped using 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>();

Running this query gives

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

I guess this is because I use Select, not SelectGroupin my predictions. I tried .SelectGroup(xx => new SqlFunctionProjection("date", NHibernateUtil.Date, Projections.Group<Domain.Locate>(g => g.WorkToBeginDate))), however it gives me Could not determine member from new SqlFunctionProjection("date", NHibernateUtil.Date, new [] {Group(g => Convert(g.WorkToBeginDate))}).

+3
source share
1 answer

You can insert the dateGroupBy value by another method Projections.Group.

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

This will give you the desired results. :)

+3
source

All Articles