NHibernate criteria query that sums field from collection
I have a simple one-to-many relationship between table Order and table Line.
I want a query that reports each Order entity and the sum of the Line.Value field.
I can do this in HQL:
select order, sum(line.Value) as LineValue
from Order a开发者_如何学编程s order
join order.Lines as line
group by order
So far, so good. When I run this I get a List of object arrays where [0] is the Order and [1] is the sum of the line values.
How do I do the same thing with the criteria API?
The closest I can get is something like:
session.CreateCriteria(typeof(Order))
.CreateAlias("Lines", "l")
.SetProjection(Projections.ProjectionList()
.Add(Projections.GroupProperty("Id"))
.Add(Projections.GroupProperty("Customer"))
/* ... ditto for each Order field ... */
.Add(Projections.Sum("l.Value"))
I have to manually add each property off Order that I want to return. How do I specify that I want to group by all the Order fields?
精彩评论