Nhibernate QueryOver - 如何按月分组?

时间:2012-05-07 09:22:18

标签: nhibernate queryover

尝试获取按月分组的总行数并与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>();

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:4)

您可以使用datepart函数have a look here查看示例。只是在这里写一些:

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

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

答案 2 :(得分:0)

我没有尝试自己,只是将答案放在other questions上:

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))