c# - NHibernate Criteria Where any element of list property is true -


i have created nhibernate criteria query need modified can add new condition.

the query based on order object, has list of orderitems , then, every orderitem has bool property named finaldeliveryindicator.

in criteria query need add condition in want orders @ least 1 of orderitems has finaldeliveryindicator bool set true.

the query, @ moment, is:

var search = nhibernatesession.createcriteria(typeof(order))             .createalias("contract", "c", jointype.innerjoin)             .createalias("c.supplier", "s", jointype.innerjoin)             .add(restrictions.eq("buyer.id", companyid))             .add(restrictions.eq("isdeleted", false))             .add(restrictions.eq("isactiveversion", true))             .setfirstresult(paging.pageindexsql)             .setmaxresults(paging.pagesize)             .addorder(sortorder.desc("id")); 

now need add condition told about. query in use @ many places on application , cannot switch queryover or other type of query, due risk of crashing something.

what need sub-select. achieved subquery.

15.8. detached queries , subqueries

we can define subquery detachedcriteria:

var subquery = detachedcriteria.for<orderitem>()     .add(restrictions.eq("finaldeliveryindicator", true))     .setprojection(projections.property("orderid")); 

this later end sql snippet:

(select orderid orderitems finaldeliveryindicator = 1 ) 

and subquery can use part of in our main query

... search.add(subqueries.propertyin("id", subquery)) ... 

which add restriction clause:

select ... order this_ join ... ... , this_.orderid in // above subquery         (select orderid orderitems finaldeliveryindicator = 1 ) 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -