即使使用DefaultIfEmpty,Linq查询也不会返回预期结果
我的一个实体框架核心API控制器中有以下查询:即使使用DefaultIfEmpty,Linq查询也不会返回预期结果,linq,asp.net-core,entity-framework-core,asp.net-core-2.0,iqueryable,Linq,Asp.net Core,Entity Framework Core,Asp.net Core 2.0,Iqueryable,我的一个实体框架核心API控制器中有以下查询: var plotData = await (from nl in _context.BookList join ql in _context.PlotList on nl.PlotId equals ql.PlotId join qc in _context.PlotChoices on ql.PlotId equals qc.PlotId join nk in _context.BookLinks.DefaultIfEmpty() o
var plotData = await (from nl in _context.BookList
join ql in _context.PlotList on nl.PlotId equals ql.PlotId
join qc in _context.PlotChoices on ql.PlotId equals qc.PlotId
join nk in _context.BookLinks.DefaultIfEmpty() on qc.ChoiceId equals nk.ChoiceId
where nl.Id == ID
select new
{ .. }
我需要它返回所有行,即使BookLinks表中不存在数据
但是,如果BookLinks表中没有该行的数据,则不会返回该行
但这个SQL查询(我试图从中建模)确实返回数据……如果BookLink中没有数据,它将返回null
select * from BookList bl
left join PlotList pl ON bl.plotId = bl.plotId
left join PlotChoices pc ON pl.plotId = pc.plotId
left join BookLinks bk ON pc.choiceID = bk.choiceID
where nl.caseID = '2abv1'
根据我在网上看到的,在BookLinks的末尾添加'DefaultIfEmpty'应该可以解决这个问题,但事实并非如此
我做错了什么
谢谢 使用left join时,您可以尝试以下代码示例:
var plotData = (from nl in _context.BookList
join ql in _context.PlotList on nl.PlotId equals ql.PlotId
join qc in _context.PlotChoices on ql.PlotId equals qc.PlotId
join nk in _context.BookLinks on qc.ChoiceId equals nk.ChoiceId into Details
from m in Details.DefaultIfEmpty()
where nl.Id == ID
select new
{
}).ToList();
BookLinks是唯一一个没有记录的实体还是其他实体?也许我的建议可以帮助您。