问题
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:
- 15/01/2012 200
- 16/01/2012 200
- 15/02/2012 300
- 16/02/2012 300
- 15/03/2012 400
- 16/03/2012 400
I will get:
- 01/2012 400
- 02/2012 600
- 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