Search code examples
c#nhibernatehibernate-criterianhibernate-projections

Sum of data for current week using projection queries


I want to do a total of fields in database for the current week. For eg, If today is wednesday, I want to do a total of current Monday through Wednesday, if its Thursday then Monday through Thursday.. How will I do this using projection queries in NHibernate? In my below code how will I group the data so it displays sum of current week only. The field in database for date is named MyDate and it is of type 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"));

Solution

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