Linq to Entities - 左外连接与Lambda表达式

时间:2011-10-27 11:43:59

标签: .net c#-4.0 lambda linq-to-entities

我有这个查询,我无法在Lambda表达式中找到这个。

select * from NewsVersion nv
left outer join ChangeProcess cp on cp.DocumentId = nv.NewsId and cp.EndDate is null
where nv.NewsId = 'B2301B7F-D37E-4CF5-9392-01844564BFCC'

有人有想法吗?

由于

3 个答案:

答案 0 :(得分:0)

由于它不是等值连词,因此您无法使用join关键字,但您仍然可以这样做:

var query =
    from nv in db.NewsVersion
    from cp in db.ChangeProcess.Where(c => nv.NewsId == cp.DocumentId && c.EndDate == null).DefaultIfEmpty()
    where nv.NewsId = "B2301B7F-D37E-4CF5-9392-01844564BFCC"
    select new { NewsVersion = nv, ChangeProcess = cp };

答案 1 :(得分:0)

这样的事情怎么样......

var query =
    from nv in NewsVersion
    from cp in ChangeProcess.DefaultIfEmpty()
    where nv.NewsId == cp.DocumentId && cp.EndDate == null && 
       nv.NewsId = "B2301B7F-D37E-4CF5-9392-01844564BFCC"
    select new { ... }

答案 2 :(得分:0)

我不是百分百肯定而且有点困惑为什么托马斯·莱维斯克认为这不是一个等值连接(毕竟NewsId == DocumentId是一个相等比较),但我不明白为什么这不起作用:

var query =
from nv in db.NewsVersion
join cp in db.ChangeProcess on nv.NewsId equals cp.DocumentId into joined
from j in joined.Where(x => x.EndDate == null).DefaultIfEmpty()
where nv.NewsId = "B2301B7F-D37E-4CF5-9392-01844564BFCC"
select new { NewsVersion = nv, ChangeProcess = j };

编辑:根据OP的评论更正