gpt4 book ai didi

NHibernate QueryOver 多个表的联接语法

转载 作者:行者123 更新时间:2023-12-02 00:29:59 28 4
gpt4 key购买 nike

如何使用 QueryOver 重写此 SQL?我不确定 QueryOver 的连接优先级和解析是如何工作的。

SELECT DISTINCT T1.*
FROM T1
LEFT JOIN T2
JOIN T3
ON T2.T3Key = T3.PrimaryKey
ON T1.PrimaryKey = T2.T1Key
LEFT JOIN T4
JOIN T5
ON T4.T5Key = T5.PrimaryKey
ON T1.PrimaryKey = T4.T1Key
WHERE T3.Criteria = @Criteria
OR T5.Criteria = @Criteria

http://www.sqlfiddle.com/#!3/affd13/5

最佳答案

假设您已设置所有关系,只需设置要在 QueryOver 中使用的别名变量...

T2 t2Alias = null;
T3 t3Alias = null;
T4 t4Alias = null;
T5 t5Alias = null;

int criteria = 1;

var results = Session.QueryOver<T1>()
.Left.JoinAlias(x => x.T2, () => t2Alias)
.Left.JoinAlias(() => t2Alias.T3, () => t3Alias)
.Left.JoinAlias(x => x.T4, () => t4Alias)
.Left.JoinAlias(() => t4Alias.T5, () => t5Alias)
.Where(Restrictions.Disjunction()
.Add(Restrictions.Where(() => t3Alias.Criteria == criteria))
.Add(Restrictions.Where(() => t5Alias.Criteria == criteria)))
.TransformUsing(Transformers.DistinctRootEntity)
.List();

我认为您无法嵌套这些内部联接...但看起来您会通过所有左联接获得相同的结果。

关于NHibernate QueryOver 多个表的联接语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25312088/

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