在NHibernate 3.2中加入linq

时间:2012-11-29 11:38:52

标签: linq nhibernate left-join

是否可以在NHibernate 3.2中的linq中实现左连接?

我想获得类似于这个SQL查询的linq查询:

select v.*, cp.EffectiveStart
from Visits v
join VisitServices vs on v.Id = vs.VisitId
left join CarePlans cp on cp.Id = vs.CarePlanId

我写了这样的linq查询:

var c = (from v in EntitiesRepository
                     join vs in _visitServiceRepository on v.Id equals vs.Visit.Id
                     join cp in _carePlanRepository on vs.CarePlan.Id equals cp.Id into pp                     
                     from pl in pp.DefaultIfEmpty()
                     select new { Visit = v, EffectiveStart = pl.EffectiveStart}).ToList();

但我得到了这个例外

  

未实施方法或操作。

答案: 我能够使用导航属性解决问题:

    var c = (from v in EntitiesRepository
             join vs in _visitServiceRepository on v.Id equals vs.Visit.Id
             select new { Visit = v, EffectiveStart = vs.CarePlan == null ? null : (DateTime?)vs.CarePlan.EffectiveStart}).ToList();

2 个答案:

答案 0 :(得分:4)

目前仅在导航属性上支持外部联接。例如:

from child in parent.Children.DefaultIfEmpty() 

编辑:对不起,好像不是3.2。你不能更新吗?

答案 1 :(得分:2)

我能够使用导航属性解决问题:

    var c = (from v in EntitiesRepository
             join vs in _visitServiceRepository on v.Id equals vs.Visit.Id
             select new { Visit = v, EffectiveStart = vs.CarePlan == null ? null : (DateTime?)vs.CarePlan.EffectiveStart}).ToList();