QueryOver - 向Join添加其他条件

时间:2012-03-02 16:27:21

标签: c# sql nhibernate queryover join

我对NHibernate和QueryOver很陌生,我无法让NHibernate生成我需要的SQL。

我需要进行加入并有一个额外的标准,所以我避免从我加入的表中获取大量数据。

我从QueryOver收到的SQL是:

SELECT * FROM adresse this_ 
left outer join r580_test.afvigelse remarkalia1_ on this_.id=remarkalia1_.adrid 
left outer join r580_test.afvigelseklagepunkter remarkcomp5_ on remarkalia1_.id=remarkcomp5_.afvigelseid 
left outer join r580_test.klagepunkter complainta2_ on remarkcomp5_.klagepunktid=complainta2_.id 
WHERE  this_.id = 16633 and remarkalia1_.dato between '2009-03-13 00:00:00' and '02-03-2012 16:34:35'

我想要的是这个(第一个左外连接之间的日期已经移到了最后):

SELECT * FROM adresse this_ 
left outer join r580_test.afvigelse remarkalia1_ on this_.id=remarkalia1_.adrid and remarkalia1_.dato between '2009-03-13 00:00:00' and '02-03-2012 16:34:35'
left outer join r580_test.afvigelseklagepunkter remarkcomp5_ on remarkalia1_.id=remarkcomp5_.afvigelseid 
left outer join r580_test.klagepunkter complainta2_ on remarkcomp5_.klagepunktid=complainta2_.id 
WHERE  this_.id = 16633

我的QueryOver看起来像这样:

adr = session.QueryOver<Address>()
    .Where(x => x.Id == 16633)
    .JoinQueryOver<Remark>(y => y.Remarks).Where(y => y.Created > DateTime.Now.AddDays(-14))
    .JoinAlias(y => y.RemarkComplaint, () => complaintAlias, JoinType.LeftOuterJoin)
    .SingleOrDefault();

任何人都知道如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

joinqueryover有几个重载 - 我相信你想要的东西:

Remark remark = null;
adr = session.QueryOver<Address>()
    .Where(x => x.Id == 16633)
    .JoinQueryOver<Remark>(y => y.Remarks, () => remark, y => y.Created > DateTime.Now.AddDays(-14))
    .JoinAlias(y => y.RemarkComplaint, () => complaintAlias, JoinType.LeftOuterJoin)
    .SingleOrDefault();

在这种情况下,第三个参数是withClause,我相信,它会将限制添加到连接中。