我有以下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
条件是连接的一部分而不是在WHERE子句中?因为对于某些行来说,f
可能不存在,但我仍然希望包括这些行。如果将条件应用于JOIN之后的WHERE子句中,则无法获得所需的行为。
不幸的是,以下内容:
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
这并不完全符合我的要求。