NHibernate QueryOver with subquery or other ideas how this can work? -
i have following queries:
model.rampactivehour rah = null; var defaultweekquery = queryover.of<model.rampadditionaldefaultweek>() .where(adw => adw.ramp == rah.ramp && adw.active && adw.fromdate <= date && adw.todate >= date) .select(adw => adw.id).take(1); var result = session.queryover(() => rah) .where(ah => ah.dayofweek == date.dayofweek) .whererestrictionon(ah => ah.ramp).isin((icollection) ramps) .withsubquery.whereproperty(ah=>ah.additionaldefaultweek) .eq(defaultweekquery) .list();
the result query is:
select this_.id id3_0_, this_.dayofweek dayofweek3_0_, this_.active active3_0_, this_.slotscount slotscount3_0_, this_.slotid slotid3_0_, this_.slotlength slotlength3_0_, this_.date date3_0_, this_.ramp_id ramp8_3_0_, this_.additional_default_week_id additional9_3_0_, this_.previous previous3_0_, this_.next next3_0_ rampactivehour this_ this_.dayofweek = 3 /* ?p0 */ , this_.ramp_id in ( 3484 /* ?p1 */, 3498 /* ?p2 */) , this_.additional_default_week_id = ( select this_0_.id y0_ rampadditionaldefaultweek this_0_ ( ( ( this_0_.ramp_id = this_.ramp_id , this_0_.active = 1 /* ?p103 */) , this_0_.fromdate <= '2011-07-20t00:00:00.00' /* ?p104 */) , this_0_.todate >= '2011-07-20t00:00:00.00' /* ?p105 */) limit 1 /* ?p106 */)
the query correct. problem in model have property in rampactivehour called additionaldefaultweek , property mapped rampadditionaldefaultweek table many-to-one. many 1 relation can null (which means there not additional default week) or can set int (which mean there active additional default week).
the problem if there no active additionaldefaultweek subquery returns empty set , reason whole query return empty set.
i thought projections.conditional projections, still can make work.
any appreciated.
thanks.
var result = session.queryover(() => rah) .where(ah => ah.dayofweek == date.dayofweek) .whererestrictionon(ah => ah.ramp).isin((icollection) ramps) .withsubquery.whereproperty(ah=>ah.additionaldefaultweek).eq(defaultweekquery) .where(new disjunction() .add(subqueries.whereproperty(ah=>ah.additionaldefaultweek).eq(defaultweekquery)) .add(new conjunction() .add(subqueries.wherenotexists(defaultweekquery)) .add(restrictions.where(ah=>ah.additionaldefaultweek == null))) .list();
i'm not fluent in query over, there might better syntax
Comments
Post a Comment