复杂的NHibernate的QueryOver表达 [英] Complex nHibernate QueryOver expression
问题描述
我有一个层次 A>的下列对象; B> C> ð
。每个对象被映射到表中。我想写使用QueryOver的SQL语句:
I have the following objects in a hierarchy A > B > C > D
. Each object is mapped to a table. I'm trying to write the following SQL using QueryOver:
SELECT B
FROM A, B, C, D
WHERE A.ID = B.ID
AND B.ID = C.ID
AND C.ID = D.ID
WHERE A.NUMBER = 'VALUE'
AND D.NAME IN ('VALUE1', 'VALUE2')
我的C#代码到目前为止:
I have the C# code so far:
string[] entityNames = entityAttributes.Select(e => e.Name).ToArray();
string customerNumber = 2;
return session.QueryOver<B>()
.JoinQueryOver(b => b.C)
.JoinQueryOver(c => c.D)
.WhereRestrictionOn(d => d.Name).IsIn(entityNames)
.List<B>();
现在缺少的这里是 A>乙
链接。我无法弄清楚如何添加加盟 A
限制它放在 NUMBER
字段。我尝试以下,但 .JoinQueryOver(B => BC)
正在寻求键入 A
,而不是寻找类型 b
What's missing here is the A > B
link. I cannot figure out how to add the join to A
restricting it on the NUMBER
field. I tried the following but the .JoinQueryOver(b => b.C)
is looking for type A
instead of finding type B
.
return session.QueryOver<B>()
.JoinQueryOver(b => b.A)
.Where(a => a.Number == customerNumber)
.JoinQueryOver(b => b.C) **//Looks for type A instead of B**
.JoinQueryOver(c => c.D)
.WhereRestrictionOn(d => d.Name).IsIn(entityNames)
.List<B>();
我如何添加类型 A
此查询,同时还返回类型 b
?
How can I add type A
to this query while still returning type B
?
推荐答案
您可以使用别名来加入你的表像
you can use Aliases to join your table like
B tB = null;
A tA = null;
C tC = null;
D tD = null;
var qOver = HibSession.QueryOver<B>(() => tB)
.JoinAlias(() => tB.A, () => tA, JoinType.LeftOuterJoin)
.Where(() => tA.Number == customerNumber)
.JoinAlias(() => tB.C, () => tC, JoinType.LeftOuterJoin)
.JoinAlias(() => tC.D, () => tD, JoinType.LeftOuterJoin)
.Where(Restrictions.On(() => tD.Name).IsIn(entityNames))
.List<B>();
我希望这是有帮助的。
I hope it's helpful.
这篇关于复杂的NHibernate的QueryOver表达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!