有没有办法在不涉及交叉连接的情况下使用Entity Framework执行“菱形连接”?在SQL本身中,我可以这样说:
SELECT d.*
FROM a
LEFT JOIN b ON a.x = b.x
LEFT JOIN c ON a.y = c.y
LEFT JOIN d ON b.z = d.z OR c.z = d.z
WHERE a.w = 42;这避免了与d的交叉连接。
对于实体框架,以下LINQ语法等效,但执行与d的交叉联接
from a in TableA
join b in TableB on a.x equals b.x into bGroup
from b in bGroup.DefaultIfEmpty()
join c in TableC on a.y equals c.y into cGroup
from c in cGroup.DefaultIfEmpty()
from d in TableD
where d.z == b.z || d.z == c.z
where a.w == 42
select d;(我假设内部连接与此演示代码中的这些左外部连接的行为相同;我没有检查,因为我正在处理的查询使用了左连接。)
发布于 2020-07-29 00:06:03
尝试以下操作:
(from a in TableA
join b in TableB on a.x equals b.x into bGroup
from b in bGroup.DefaultIfEmpty()
join c in TableC on a.y equals c.y into cGroup
from c in cGroup.DefaultIfEmpty()
select new { a = a, b = b, c = c}
).SelectMany(x => TableD.Where(y => (x.a.w == 42) && ((x.b.z == y.z) || (x.c.z == y.z))).Select(y => y))
.ToList(); https://stackoverflow.com/questions/63137828
复制相似问题