简体   繁体   English

复杂的nHibernate QueryOver表达式

[英]Complex nHibernate QueryOver expression

I have the following objects in a hierarchy A > B > C > D . 我在层次结构A > B > C > D有以下对象。 Each object is mapped to a table. 每个对象都映射到一个表。 I'm trying to write the following SQL using QueryOver: 我正在尝试使用QueryOver编写以下SQL:

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')

I have the C# code so far: 到目前为止,我有C#代码:

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

What's missing here is the A > B link. 这里缺少的是A > B链接。 I cannot figure out how to add the join to A restricting it on the NUMBER field. 我无法弄清楚如何将连接添加到A限制它在NUMBER字段上。 I tried the following but the .JoinQueryOver(b => bC) is looking for type A instead of finding type B . 我尝试了以下但.JoinQueryOver(b => bC)正在寻找类型A而不是查找类型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>();

How can I add type A to this query while still returning type B ? 如何在仍返回类型B同时向此查询添加类型A

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. 我希望它有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM