我试图将下面的SQL连接到LINQ c#中的三个SQL表中
SELECT
rpp.*
FROM dbo.Orgs ao
LEFT JOIN dbo.Afflia rpa
ON rpa.AccountId = ao.ID
INNER JOIN dbo.reports rpp
ON rpp.Id = rpa.reporttId
WHERE ao.Name like '%xyz%'上面的查询返回数据,但是等效的LINQ查询没有如下所示
from a in context.Orgs
join aff in context.Afflia on a.ID equals aff.AccountId
join prescriber in context.Reports on aff.reportId equals prescriber.Id
where a.ORG_NAME.Contains("xyz"),我能知道错误在哪里吗?
发布于 2017-08-28 19:35:06
在您的SQL中,您正在对dbo.Afflia执行左联接,但在LINQ中,您正在执行内部连接。您需要添加"DefaultIfEmpty()“,例如
from aff in context.Afflia.Where(join condition here).DefaultIfEmpty()发布于 2017-08-28 20:25:09
在LINQ中,您做了内部连接,但是在SQL中,您确实离开了join。
试一试:
from a in context.Orgs
join aff in context.Afflia on a.ID equals aff.AccountId into affs
from aff in affs.DefaultIfEmpty()
join prescriber in context.Reports on aff.reportId equals prescriber.Id
where a.ORG_NAME.Contains("xyz")发布于 2017-08-28 19:40:47
你可以这样做:
var prescribers = (from a in context.Orgs
from aff in context.Afflia.Where(aff => aff.AccountId == a.ID)
from prescriber in context.Reports.Where(pres => pres.Id == aff.reportId)
where a.ORG_NAME.Contains("xyz")
select prescriber)
.ToList();https://stackoverflow.com/questions/45926084
复制相似问题