今天我遇到了Entity Framework的一个问题。我不确定这是一个奇怪的错误还是我做错了什么。我已经在整个论坛上寻找任何可能的解决方案,但没有一个我发现为我工作。LINQ查询中没有包含哪里
我有以下LINQ查询:
return (from sp in context.ServiceProviders.DefaultIfEmpty()
join pl in context.Platforms on sp.Id equals pl.ServiceProviderId into innerPl
from pl in innerPl.DefaultIfEmpty()
join pp in context.Participants on pl.Id equals pp.PlatformId into innerPp
from pp in innerPp.DefaultIfEmpty()
join ps in context.Paymentsettlements on pp.Id equals ps.ParticipantId into innerPs
from ps in innerPs.Where(ps => ps.ConfirmedOn.HasValue && ps.ExportDate.HasValue && !ps.StatisticsDate.HasValue).DefaultIfEmpty()
select sp).Include(sp => sp.Environment)
.Include(sp => sp.Platforms.Select(pl => pl.Participants.Select(pp => pp.Paymentsettlements.Select(ps => ps.Requester))))
.Include(sp => sp.Platforms.Select(pl => pl.Participants.Select(pp => pp.Paymentsettlements.Select(ps => ps.Payer))))
.ToList();
我正在寻找的结果是,我总是得到的ServiceProvider不管是否有在里面的ServiceProvider对象。目前我正在得到这个结果,但是我在查询中的位置并没有被考虑在内。以下哪里没有任何区别:
innerPs.Where(ps => ps.ConfirmedOn.HasValue && ps.ExportDate.HasValue && !ps.StatisticsDate.HasValue).DefaultIfEmpty()
如果StatisticsDate有一个值,那么这些Paymentsettlements也会在输出中给出。
我已经尝试将WHERE语句放在context.Paymentsettlements对象上。
我希望任何人都可以帮助我解决这个问题。
亲切的问候,
罗布^ h
您是否需要'LEFT OUTER JOIN'?因为,你所做的是左连接。 – 2015-02-23 16:10:56
是的,我总是希望所有对象(它只需要筛选不符合WHERE子句要求的Paymentsettlements,但如果没有Paymentsettlements,我确实需要上述对象) – RobH 2015-02-23 16:12:09
不确定,但可能是因为懒惰的执行。 尝试在where语句之后添加.ToList()。 (即 - innerPs.Where(ps => ps.ConfirmedOn.HasValue && ps.ExportDate.HasValue &&!ps.StatisticsDate.HasValue).ToList()。DefaultIfEmpty() – 2015-02-23 16:16:02