简体   繁体   English

如何使用NHibernate离开联接两个不同的表

[英]How to left join two different tables using NHibernate

I have three tables (Table A, B and C). 我有三个表(表A,B和C)。 I would like to do the following: Left Join A with B and Left join A with C. 我想执行以下操作:左连接A与B,左连接A与C。

Now I have used CreateCriteria, to do the joins using jointype which worked upto a certain point but it's throwing Query exception. 现在,我已经使用CreateCriteria来使用jointype进行联接,该联接可以工作到一定程度,但是会引发Query异常。 It seems this is because it's seems to attempt to left join B with C rather than A and C. 看来这是因为似乎要使B与C而不是A和C保持连接。

Code: 码:

currencies = session.CreateCriteria(typeof(Currency), "TableA")                 
            .CreateCriteria("FXRates", "TableB",
                            JoinType.LeftOuterJoin, 
                            Expression.Eq("fxrate.RateDate",date))
            .CreateCriteria("FundingRates", "TableC", 
                            JoinType.LeftOuterJoin,
                            Expression.Eq("fundingrate.RateDate", date))
            .Add(Restrictions.IsNotNull("currency.code"))
            .List<Currency>();

Apologies in advanced if I have missed out anything or not provided enough detail, let me know if you need more... 如果您有任何遗漏或未提供足够详细的信息,请向我们道歉,如果需要更多信息,请告诉我...

You can use NHibernate "QueryOver" to do it: 您可以使用NHibernate“ QueryOver”来做到这一点:

session.QueryOver<Item_A>()
   .Left.JoinQueryOver(item_A => item_A.Item_B)
   .Left.JoinQueryOver(item_A => item_A.Item_C)
   .TransformUsing(Transformers.DistinctRootEntity)
   .List();

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

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