Help with QueryOver and WhereExists

送分小仙女□ 提交于 2019-12-20 20:19:09

问题


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( ??? );

回答1:


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>();


来源:https://stackoverflow.com/questions/5194900/help-with-queryover-and-whereexists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!