帮助QueryOver和WhereExists [英] Help with QueryOver and WhereExists
问题描述
我有一个问题。我有人员和猫。每个人都有一些猫(有猫的外键指向人的主键)。每只猫都有一个时代。我要选择有老猫的个人。我想这些人的所有的猫,而不是只有老猫。
我需要与QueryOver语法来做到这一点。
在T-SQL它会是这样的:
SELECT * P.,C *
从人p
LEFT JOIN猫C
关于P.Id = C.OwnerId
在存在(
选择1
FROM猫C2
其中P.Id = C2.OwnerId和C2.Age→5)
我知道我必须使用子查询,我可以很容易地与旧的NHibernate语法(条条/的DetachedCriteria),但我不能做到在QueryOver语法。
我不希望入状态。我的主键是一个复杂的关键,所以我不能用IN做到这一点。
变种人= session.QueryOver<人> .WithSubquery.WhereExists(???);
从的this页面和调整(用我自己的类测试):
诀窍似乎在用。别名
人员personAlias = NULL;
&IList的LT;人>者=
session.QueryOver&所述;人>(()=> personAlias).WithSubquery
.WhereExists(QueryOver.Of&所述;目录>()
。凡(C =&℃。年龄大于5)
。而(C => c.Owner.Id == personAlias.Id)
。选择(C => c.Owner))
&的.List LT;人>();
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( ??? );
Example taken from this page and adapted (tested with my own classes):
The trick seems to be using an alias.
Person personAlias = null;
IList<Person> persons =
session.QueryOver<Person>(() => personAlias).WithSubquery
.WhereExists(QueryOver.Of<Cat>()
.Where(c => c.Age > 5)
.And(c => c.Owner.Id == personAlias.Id)
.Select(c => c.Owner))
.List<Person>();
这篇关于帮助QueryOver和WhereExists的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!