【发布时间】:2010-11-10 12:16:52
【问题描述】:
我有以下 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
这不是我想要的。
【问题讨论】:
-
甜蜜!我一直在寻找这个,但不知道如何搜索这个。不知道如何在这个答案中添加标签。这是我使用的搜索条件: linq to sql filter in join 或 from linq to sql where 子句 in join 或 from
标签: c# sql linq linq-to-sql outer-join