有关 QueryOver 和WhereExists 的帮助
我有一个问题。我有人和猫。每个人都有一些猫(猫中有一个外键指向人中的主键)。每只猫都有一个年龄。我想选择拥有“老”猫的人。我想要这些人的所有猫,而不仅仅是“老”猫。 我需要使用 QueryOver 语法来完成此操作。
在 T-SQL 中,它会是这样的:
SELECT P.*, C.*
FROM Persons P
LEFT JOIN Cats C
ON P.Id = C.OwnerId
WHERE EXISTS (
SELECT 1
FROM Cats C2
WHERE P.Id = C2.OwnerId AND C2.Age > 5)
我知道我必须使用子查询,并且我可以轻松地使用“旧”nhibernate 语法(Criteria/DetachedCriteria),但我不能用 QueryOver 语法来做到这一点。
我不想要“IN”状态。我的主键是一个复杂的键,所以我无法使用 IN 来完成它。
var persons = session.QueryOver<Person>.WithSubquery.WhereExists( ??? );
I have a problem. I have Persons and Cats. Each Person has some Cats (there is a foreign key in Cats that points to the primary key in Persons). Each Cat has an Age. I want to select the Persons that have "Old" Cats. I want ALL the Cats of these persons, and not only the "Old" Cats.
I need to do it with the QueryOver syntax.
In T-SQL it would be something like:
SELECT P.*, C.*
FROM Persons P
LEFT JOIN Cats C
ON P.Id = C.OwnerId
WHERE EXISTS (
SELECT 1
FROM Cats C2
WHERE P.Id = C2.OwnerId AND C2.Age > 5)
I know I have to use the subqueries, and I could to easily with the "old" nhibernate syntax (the Criteria/DetachedCriteria), but I can't do it in QueryOver syntax.
I DON'T want an "IN" condition. My Primary Key is a complex key, so I can't do it with the IN.
var persons = session.QueryOver<Person>.WithSubquery.WhereExists( ??? );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
示例取自此页面并进行改编(用我自己的类进行测试):
这个技巧似乎是使用别名。
Example taken from this page and adapted (tested with my own classes):
The trick seems to be using an alias.