gpt4 book ai didi

NHibernate QueryOver 与子查询或其他想法如何工作?

转载 作者:行者123 更新时间:2023-12-04 23:07:53 27 4
gpt4 key购买 nike

我有以下疑问:

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 表。这种多对一关系可以为空(这意味着没有额外的默认周)或可以设置为 int(这意味着有事件的额外默认周)。

问题是,如果没有事件的 AdditionalDefaultWeek,子查询返回空集,因此整个查询返回空集。

我想到了 Projections.Conditional 投影,但仍然可以完成这项工作。

任何帮助深表感谢。

谢谢。

最佳答案

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();

我不太擅长查询,所以可能有更好的语法

关于NHibernate QueryOver 与子查询或其他想法如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6768700/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com