Help with QueryOver and WhereExists
Asked Answered
M

1

12

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( ??? );
Mcgary answered 4/3, 2011 at 14:18 Comment(0)
R
28

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>();
Roos answered 4/3, 2011 at 16:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.