Pascal
Pascal

Reputation: 2214

nHibernate: Subqueries.WhereIn

So I am trying to create a query, which limits the number of results with a subquery.

Select * From Products Where products.supplierid IN (Select supplierid FROM supplier WHERE active = 1)

Since I use multiple subquery where conditions, I first tried to do something like this:

mainQuery.And(
   Restrictions.Disjunction()
   .Add(Subqueries.WhereExists(subqueryFilterA))
   .Add(Subqueries.WhereExists(subqueryFilterB))
);

This works fine, except I do not want a WhereExists but a WhereIn. But WhereIn does not seem to exist.

The only workaround I found is:

mainQuery.Where(
   Restrictions.Disjunction()
   .Add(Subqueries.PropertyIn("products.supplierId", subqueryFilterA))
);

This seems to work alright. But since the subqueryFilterA is now a DetachedCriteria I can no longer use linq.

Is there a reason why there is no Subqueries.WhereIn?

Upvotes: 0

Views: 211

Answers (1)

Roman Artiukhin
Roman Artiukhin

Reputation: 2367

Use Subqueries.WhereProperty:

Subqueries
  .WhereProperty(() => products.supplierId)
  .In(QueryOver.Of<T>.Where(...).Select(...))

Upvotes: 2

Related Questions