使用 NHibernate 连接子查询
是否可以在 Criteria 或 QueryOver (NHibernate 3.1) 中执行以下查询?
SELECT
C.CustomerID, C.CustomerName,
C.CustomerType, C.Address1, C.City,
C.State, S.TotalSales
FROM
Customers C
INNER JOIN
(SELECT
CustomerID, SUM(Sales) as TotalSales
FROM
Sales
GROUP BY
CustomerID) S
ON
C.CustomerID = S.CustomerID
有一个类似的问题,但它很旧并且从未得到解答。也许随着 NH 团队最近的重大更新,这个问题可以得到答案! NHibernate 2.1:带别名的子查询上的 LEFT JOIN (ICriteria)谢谢
Is it possible to perform the following query in Criteria or QueryOver (NHibernate 3.1)?
SELECT
C.CustomerID, C.CustomerName,
C.CustomerType, C.Address1, C.City,
C.State, S.TotalSales
FROM
Customers C
INNER JOIN
(SELECT
CustomerID, SUM(Sales) as TotalSales
FROM
Sales
GROUP BY
CustomerID) S
ON
C.CustomerID = S.CustomerID
There was a similar question but it's quite old and was never answered. Maybe with the recent major updates from the NH team this can be answered!
NHibernate 2.1: LEFT JOIN on SubQuery with Alias (ICriteria)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果对象模型中的 Customer 和 Sales 之间没有关系,那么您无法使用我能想到的 NH2.1 中的任何查询方法将这两个对象连接在一起。
此外,您不能加入不相关实体的子查询,如您的示例中所示。
不过,您可以在 NH2.1 中执行此操作,这将得到类似的结果。
这将对服务器进行一次往返,发出两个查询,一个针对所有客户,另一个针对具有 customerid 的销售总额。
然后,您可以使用 LINQ 连接内存中的两个结果集。
If there is no relationship between Customer and Sales in the object model then you cannot join the two object together using any query methods in NH2.1 that I can think of.
Also you cannot join subqueries of unrelated entities, like in your example.
You can however do this in NH2.1 which will give you similar results.
This will do one round trip to the server issuing two queries, one for all customers and one for a aggregate of sales with the customerid.
Then you can join the two result sets in memory using LINQ.
我的 50 美分 ->您需要更改对象模型。这样一个客户就包含了销售。
您的查询如下所示,它更加简洁并且更好地面向对象。虽然我不知道如何执行上面的查询,但它违背了使用 ORM 工具的目的。
My 50 cents -> You need to change your object model. So that a customer contains sales.
Your query when then look like the following, which is far more terse and better object orientated. I dont know how to do the query above though but it defeats the purpose of using an ORM tool.