linq - Entity Framework - 如何在多个键上使用 OR 条件进行连接

标签 linq join entity-framework-4

在两列上连接表格很容易

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/

相关文章:

c# - 删除 LINQ to EF 中的关联数据

sql - 在 awaitResult 中抛出 SPARK 异常

c# - 使用Entity Framework 为什么要重新启动DbContext?

c# - Entity Framework 4.0 - 版本控制

c# - 通过多个值查询字段的 Linq 语句被转换为 in 语句

c# - 修改属性后列出顺序更改

c# - 在通用存储库中使用 Entity Framework 默认加载 ALL

SQL 与 order by 连接

join - 如何避免星型模式中的复杂连接?

c# - Visual C# 和 SQL Server 无法一起正常工作//文件已在使用中