Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help with QueryOver and 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( ??? );
like image 335
xanatos Avatar asked Mar 04 '11 14:03

xanatos


1 Answers

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>();
like image 165
Florian Lim Avatar answered Nov 11 '22 10:11

Florian Lim