4
我有以下疑問:NHibernate的QueryOver與子查詢或其他想法如何可以工作?
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預測,但仍然可以使這項工作。
任何幫助,非常感謝。
謝謝。
感謝您的回答,但如果我理解正確的這個查詢將創建或邏輯。這意味着如果有一個活動的默認周,這個查詢將檢索默認的RampActiveHours和附加。 –
換句話說,如果子查詢爲空,則where語句爲this_.additional_default_week_id爲null,但如果子查詢不爲null,則爲this_.additional_default_week_id = number –
我按照編輯我的答案。不能說任何事無法表現 – Firo