gpt4 book ai didi

c# - NHibernate QueryOver : How to join unrelated entities?

转载 作者:行者123 更新时间:2023-12-03 06:45:07 25 4
gpt4 key购买 nike

我有以下查询可以得到我想要的结果:

int associatedId = 123;

MyObject alias = null;

var subQuery = QueryOver.Of<DatabaseView>()
.Where(view => view.AssociatedId == associatedId)
.And(view => view.ObjectId == alias.ObjectId)
.Select(view => view.ObjectId);

var results = session.QueryOver<MyObject>(() => alias)
.WithSubquery.WhereExists(subQuery)
.List();

DatabaseView已被映射为实际的 NHibernate 实体(因此我可以将它与 QueryOver 一起使用),但它不与 MyObject 关联。在 HBM 映射中。

此查询返回 IList<MyObject>使用SELECT ... FROM MyObject WHERE EXISTS (subquery for DatabaseView here) 。我如何重写它以返回相同的数据但使用 JOIN 而不是子查询?

最佳答案

在 NHibernate 5.1+ 中,可以通过 Entity Join 进行 QueryOver/Criteria :

int associatedId = 123;

MyObject alias = null;
DatabaseView viewAlias = null;

var results = session.QueryOver<MyObject>(() => alias)
.JoinEntityAlias(() => viewAlias, () => viewAlias.ObjectId == alias.ObjectId && viewAlias.AssociatedId == associatedId)
.List();

标准示例:

int associatedId = 123;
var results = session.CreateCriteria<MyObject>("alias")
.CreateEntityAlias(
"viewAlias",
Restrictions.EqProperty("viewAlias.ObjectId", "alias.ObjectId")
&& Restrictions.Eq("viewAlias.AssociationId", associatedId),
JoinType.InnerJoin,
typeof(DatabaseView).FullName)
.List();

关于c# - NHibernate QueryOver : How to join unrelated entities?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6378224/

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