gpt4 book ai didi

nhibernate - QueryOver 或与子查询

转载 作者:行者123 更新时间:2023-12-03 10:56:19 33 4
gpt4 key购买 nike

我有以下使用子查询的 NHibernate 查询:

NHContext.Session.QueryOver<Item>()
.WithSubquery.WhereProperty(x => x.ItemId).In(QueryOver.Of<Foo>().Where(x => x.AFlag).Select(x => x.ItemId))
.WithSubquery.WhereProperty(x => x.ItemId).In(QueryOver.Of<Bar>().Where(x => x.AFlag).Select(x => x.Item))
.Future<Item>();

这将运行以下 SQL:
SELECT *
FROM item this_
WHERE this_.ItemId in (SELECT this_0_.ItemId as y0_
FROM Foo this_0_
WHERE this_0_.AFlag = 1 /* @p0 */)
and this_.ItemId in (SELECT this_0_.ItemId as y0_
FROM Bar this_0_
WHERE this_0_.AFlag = 1 /* @p0 */)

我希望它使用 所以例如:
SELECT *
FROM item this_
WHERE this_.ItemId in (SELECT this_0_.ItemId as y0_
FROM Foo this_0_
WHERE this_0_.AFlag = 1 /* @p0 */)
or this_.ItemId in (SELECT this_0_.ItemId as y0_
FROM Bar this_0_
WHERE this_0_.AFlag = 1 /* @p0 */)

我知道我可以通过执行以下操作在 Criteria 中做到这一点:
var disjunction = new Disjunction();
disjunction.Add(Subqueries.PropertyIn("ItemId",
DetachedCriteria.For<Foo>()
.SetProjection(Projections.Property("ItemId"))
.Add(Restrictions.Eq("AFlag", 1))
));

但是想知道是否有一种更简单的方法可以通过 QueryOver 来实现,并避免使用字符串作为属性名称。

谢谢你的帮助。

最佳答案

对于不太常见的析取(或),我认为您需要使用 Subqueries.WhereProperty<>而不是 WithSubquery

Session.QueryOver<Item>()
.Where(Restrictions.Disjunction()
.Add(Subqueries.WhereProperty<Item>(x => x.ItemId).In(QueryOver.Of<Foo>().Where(x => x.AFlag).Select(x => x.ItemId)))
.Add(Subqueries.WhereProperty<Item>(x => x.ItemId).In(QueryOver.Of<Bar>().Where(x => x.AFlag).Select(x => x.Item))))
.Future<Item>();

关于nhibernate - QueryOver 或与子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7957733/

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