NHibernate 标准查询对集合中的字段求和
我在 Order 表和 Line 表之间有一个简单的一对多关系。
我想要一个报告每个 Order 实体和 Line.Value 字段总和的查询。
我可以在 HQL 中做到这一点:
select order, sum(line.Value) as LineValue
from Order as order
join order.Lines as line
group by order
到目前为止,一切都很好。当我运行它时,我得到一个对象数组列表,其中 [0] 是顺序,[1] 是行值的总和。
如何使用标准 API 执行相同的操作?
我能得到的最接近的是:
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 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 as 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论