我有以下问题:
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();
结果查询是:
SELECT
this_.ID as ID3_0_,
this_.DayOfWeek as DayOfWeek3_0_,
this_.Active as Active3_0_,
this_.SlotsCount as SlotsCount3_0_,
this_.SlotId as SlotId3_0_,
this_.SlotLength as SlotLength3_0_,
this_.Date as Date3_0_,
this_.ramp_id as ramp8_3_0_,
this_.additional_default_week_id as additional9_3_0_,
this_.Previous as Previous3_0_,
this_.Next as Next3_0_
FROM RampActiveHour this_
WHERE this_.DayOfWeek = 3 /* ?p0 */ and
this_.ramp_id in (
3484 /* ?p1 */,
3498 /* ?p2 */)
and
this_.additional_default_week_id = (
SELECT
this_0_.ID as y0_
FROM RampAdditionalDefaultWeek this_0_
WHERE (
(
(
this_0_.ramp_id = this_.ramp_id and
this_0_.Active = 1 /* ?p103 */)
and
this_0_.FromDate <= '2011-07-20T00:00:00.00' /* ?p104 */)
and
this_0_.ToDate >= '2011-07-20T00:00:00.00' /* ?p105 */)
limit 1 /* ?p106 */)
查询是正确的。问题是在我的模型中我在RampActiveHour中有一个名为AdditionalDefaultWeek的属性,并且该属性映射到具有多对一的RampAdditionalDefaultWeek表。这个多对一关系可以为null(这意味着没有额外的默认周)或者可以设置为int(这意味着有一个活动的附加默认周)。
问题是如果没有活动的AdditionalDefaultWeek,则子查询返回空集,因此整个查询返回空集。
我曾考虑过Projections.Conditional投影,但仍然可以使这项工作。
非常感谢任何帮助。
感谢。
答案 0 :(得分:9)
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();
我的查询不是那么流利,所以可能有更好的语法