在两列上连接表格很容易
from t1 in table1
join t2 in table2
on new { KEY1 = t1.TB1Key1, KEY2 = t1.TB1Key2 }
equals new { KEY1 = t2.TB2Key1, KEY2 = t2.TB2Key2 }
select new { t1 , t2}
但是如果我想要一个 OR 条件怎么办? SQL 看起来像这样:
select * from table1 t1
inner join table2 t2
on t1.TB1Key1 = t2.TB2Key1 OR t1.TB1Key2= t2.TB2Key2
但找不到在 EF 中执行此操作的方法,
在我的例子中,如果 t1.TB1Key1 为 null,则 t2.TB1Key 应等于 t1.TB1Key1 或 t1.TB1Key2
所以这样解决了:
from t1 in table1
join t2 in table2
on new { KEY = t1.TB1Key1 ?? t1.TB1Key2 }
equals new { KEY = t2.TB2Key}
select new { t1 , t2}
但仍然想知道是否可以在连接条件中使用 OR
最佳答案
这个怎么样:
from t1 in table1
from t2 in table2
where (t1.TB1Key1 == t2.TB2Key1 || t1.TB1Key2 == t2.TB2Key2)
select new { t1, t2 }
关于linq - Entity Framework - 如何在多个键上使用 OR 条件进行连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3861686/