group by month and year parts using queryover

自作多情 提交于 2019-12-24 00:41:01

问题


I have a model, that contains a DateTime field and a Price field. I want to get a list that gives me an aggregation of month/year - price so for the data:

  1. 15/01/2012 200
  2. 16/01/2012 200
  3. 15/02/2012 300
  4. 16/02/2012 300
  5. 15/03/2012 400
  6. 16/03/2012 400

I will get:

  1. 01/2012 400
  2. 02/2012 600
  3. 03/2012 800

So far I didn't find a way to achive this using QueryOver. I keep getting "could not resolve property" when i try to look at the Month/Year parts of the date.

Those questions:

Group posts by year, then by month

Group/Sort by Date Year/Month, Partial Select with NHibernate (Projection? Transformation?)

got answered using LINQ - not what I'm looking for.

Any ideas? Thanks

(using NHibernate 3.2)


回答1:


QueryOver is not a good choice because grouping will need to be done using a SQL expression. For this type of query, QueryOver is simply a pointless abstraction that adds no value.

If you must do it using QueryOver you could do something like:

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


来源:https://stackoverflow.com/questions/10015732/group-by-month-and-year-parts-using-queryover

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!