有很多关于 LINQ 和多重连接的帖子。
然而,我还没有找到任何我想要加入的解决方案。
SQL 等价物将是这样的:
SELECT * FROM table1 a
LEFT JOIN table2 b ON a.col1 = b.key1 AND
a.col2 = b.key2 AND
b.from_date <= now() AND
b.deleted = 0;
这是我尝试过的众多 linq 查询之一
var query = (from x in context.table1
join y in context.table2 on new {x.col1, x.col2} equals {b.key1, b.key2}
into result
from result......
如何添加日期和删除标志的附加条件?
如果我使用 .Where 条件,那么这将被视为内部联接,而不是左联接。
最佳答案
另一种方式可能就像
var query = (from x in context.table1
join y in context.table2 on
new {
Key1 = x.col1,
Key2 = x.col2,
Key3 = true,
Key4 = true
}
equals
new {
Key1 = y.key1,
Key2 = y.key2,
Key3 = y.from_date< DateTime.Now,
Key4 = !y.deleted
}
into result
from r in result.DefaultIfEmpty()
select new {x.Something, r.Something}
关于LINQ 到实体 : Multiple join conditions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7765230/