3

试图获得按月分组的总行数并与 QueryOver 作斗争。从我发现的有限资源来看,似乎线索是 Projections.SqlGroupProjection 但我无法正确理解。

这是我到目前为止所拥有的:

var jobs = _statelessSession.QueryOver<Job>()
                .SelectList(list => list
                                        .Select(Projections.SqlGroupProjection("MONTH(DateCreated) AS Month", "MONTH(DateCreated)",new string[]{"DateCreated"},new[]{NHibernateUtil.Int32}))
                                        .SelectCount(m => m))
                .List<object>();
4

3 回答 3

6

最终得到以下内容以供将来参考:

            var startDate = DateTime.Now.AddMonths(-6);
            startDate = new DateTime(startDate.Year,startDate.Month,1);
            var jobs = _session.QueryOver<Job>()
                .WhereRestrictionOn(c => c.DateCreated).IsBetween(startDate).And(DateTime.Now)
                .SelectList(list => list
                    .Select(Projections.SqlGroupProjection(
                        "YEAR(DateCreated) As [Year]",
                        "YEAR(DateCreated)",
                        new[] { "YEAR" },
                        new IType[]{NHibernateUtil.Int32}))
                    .Select(Projections.SqlGroupProjection(
                        "MONTH(DateCreated) As [Month]",
                        "MONTH(DateCreated)",
                        new[] { "Month" },
                        new IType[] { NHibernateUtil.Int32 }))
                    .SelectCount(x=>x.Id))
                .OrderBy(Projections.SqlFunction(
                       "YEAR",
                       NHibernateUtil.Int32,
                       Projections.Property<Job>(item=>item.DateCreated))).Desc
                .ThenBy(Projections.SqlFunction(
                       "MONTH",
                       NHibernateUtil.Int32,
                       Projections.Property<Job>(item => item.DateCreated))).Desc
                .List<object>();

其中产生:

SELECT YEAR(DateCreated)  As [Year],
       MONTH(DateCreated) As [Month],
       count(this_.Id)    as y2_
FROM   Jobs this_
WHERE  this_.DateCreated between '2011-11-01T00:00:00.00' /* @p0 */ and '2012-05-07T14:23:52.00' /* @p1 */
GROUP  BY YEAR(DateCreated),
          MONTH(DateCreated)
ORDER  BY datepart(year, this_.DateCreated) desc,
          datepart(month, this_.DateCreated) desc
于 2012-05-07T12:27:23.340 回答
4

您可以使用 datepart 函数,看看这里看一个例子。只是在这里写一些行:

var result = session.QueryOver<Item>()
                .Where(
                Restrictions.Eq(
                Projections.SqlFunction("month"
                , NHibernateUtil.Int32
                , Projections.Property<Item>(item => item.StartDate)
                ) 
                ,1)).List();

该示例假设 Item 实体为名为 StartDate 的属性。在帖子 中也给出了一个例子,说明如何在读取数据时使用 datepart 投影。

于 2012-05-07T10:53:48.803 回答
0

我没有尝试自己,只是将其他 问题的答案放在一起:

session.QueryOver<...>()
   .SelectList(list => list
      .Select(GroupProperty(Projections.SqlProjection(...)))
      .SelectSum(x => x.Price)
   );

+

group post by new { post.Date.Year, post.Date.Month } into grouped

=

.Select(GroupProperty(Projections.SqlProjection(
    new 
    { 
        post.Date.Year, 
        post.Date.Month 
    })))

编辑

另一个尝试:

   .SelectList(list => list
       .SelectGroup(c => c.Date.Year),
       .SelectGroup(c => c.Date.Month),
       .SelectCount(m => m))
于 2012-05-07T10:29:30.527 回答