我有以下 SQL,我正在尝试将其转换为 LINQ:
SELECT f.value
FROM period as p
LEFT OUTER JOIN facts AS f ON p.id = f.periodid AND f.otherid = 17
WHERE p.companyid = 100
我已经看到了左外连接的典型实现(即into x from y in x.DefaultIfEmpty()
等),但不确定如何引入其他连接条件(AND f.otherid = 17
)
编辑
为什么AND f.otherid = 17
条件是 JOIN 的一部分而不是 WHERE 子句中的一部分?因为f
某些行可能不存在,我仍然希望包含这些行。如果在 WHERE 子句中应用条件,则在 JOIN 之后 - 那么我不会得到我想要的行为。
不幸的是:
from p in context.Periods
join f in context.Facts on p.id equals f.periodid into fg
from fgi in fg.DefaultIfEmpty()
where p.companyid == 100 && fgi.otherid == 17
select f.value
似乎等同于:
SELECT f.value
FROM period as p
LEFT OUTER JOIN facts AS f ON p.id = f.periodid
WHERE p.companyid = 100 AND f.otherid = 17
这不是我所追求的。