sql - Nhibernate queryover filter based on count of child collection -
ultimately want filter parent objects have less 2 children.
i'm building search screen grid dashboard, uses following logic example of how build query.
var query = session.queryover<parent>(() => parentalias); if (!string.isnullorwhitespace(searchcriteria.openedby)) query.where(restrictions.eq(projections.property<parent>(x => x.openedby), searchcriteria.openedby)); if (searchcriteria.openeddatestart != null) query.where(restrictions.ge(projections.property<parent>(x => x.openedat), searchcriteria.openeddatestart));
this working wonderfully right until point:
if (!string.isnullorempty(searchcriteria.childrenaffected) && searchcriteria.childrenaffected == "multi") query.where(() => parentalias.children.count > 2);
it makes sense .count does't work, isn't linq. .count() throws error. honestly, feel i've tried every combination of restritions, joinalias, etc can think of, i've gone off path of educated attempts , realm wild guessing long time ago.
how set query filter out parent based on count of children in queryover syntax?
-----note ----- debated using linq after id gotten list, i'm doing paging in query set filter applied after page came back.
you need subquery...
children childrenalias = null; var subquery = queryover.of<children>(() => childrenalias) .where(() => childrenalias.parent.id == parentalias.id) .torowcountquery(); query.withsubquery.wherevalue(2).le(subquery);
note don't know how count > 2
, i'm doing 2 <= count
, , there possibility instead of
.where(() => childrenalias.parent.id == parentalias.id)
you can write
.where(() => childrenalias.parent == parentalias)
mmmh... if reall need count > 2 should able to:
query.where(restrictions.gt(projections.subquery(subquery), 2));
or
query.withsubquery.where(() => subquery.as<int>() > 4);
(this 1 haven't ever used... taken http://blog.andrewawhitaker.com/blog/2014/10/24/queryover-series-part-8-working-with-subqueries/)
Comments
Post a Comment