Help with QueryOver and WhereExists

2019-03-18 08:17发布

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条回答
forever°为你锁心
2楼-- · 2019-03-18 09:13

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>();
查看更多
登录 后发表回答