1

我想做本周数据库中的所有字段。例如,如果今天是星期三,我想计算当前星期一到星期三的总数,如果是星期四,那么星期一到星期四。我将如何使用 NHibernate 中的投影查询来做到这一点?在我下面的代码中,我将如何对数据进行分组,以便仅显示当前周的总和。数据库中的日期字段名为 MyDate,它的类型为 datetime。

DateTemplate.Criteria = DetachedCriteria.For(typeof(MyBill));


    DateTemplate.Criteria.SetProjection(
    Projections.ProjectionList()
          .Add(Projections.Sum(Projections.Conditional
                            (Restrictions.Eq("PayType", "ABC"),
                                  Projections.Constant(1), Projections.Constant(0))), "GCashSales")
                .Add(Projections.Sum(Projections.Conditional
                            (Restrictions.Eq("PayType", "DEF"),
                                  Projections.Constant(1), Projections.Constant(0))), "GCreditSales"));
4

1 回答 1

2

The simplest way to do this probably would be to calculate the start/end date outside of the query and add a Between restriction to your projection list.

.Add(Restrictions.Between("MyDate", startDate, endDate))
于 2011-06-01T18:10:42.037 回答