今天我遇到了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