0
votes

How can I write this query with QueryOver.

select * from User usr where exists (select ord.ID from Order ord where ord.UserID = usr.ID)

I know we can write subqueries with QueryOver like ..WithSubSelect.WhereProperty(x=>x.ID == subquery.as<int>()). But I want to use field of the main query in the subquery in order to use it in where clause.

Is this possible ?

thank you for your helps

1

1 Answers

2
votes
User userAlias = null;
var subquery = QueryOver.Of<Order>()
    .Where(o => o.User == userAlias)
    // or
    .Where(o => o.User.Id == userAlias.Id)

var usersWithOrders = session.QueryOver(() => userAlias)
    .WithSubqueries.WhereExists(subquery)
    .List();