实体框架 - 如何在多个键上加入OR条件

时间:2010-10-05 07:38:34

标签: 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中执行此操作的方法,

在我的情况下t2.TB1Key应该等于t1.TB1Key1或t1.TB1Key2如果t1.TB1Key1为空 所以这样解决了:

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

1 个答案:

答案 0 :(得分:3)

怎么样:

from t1 in table1
from t2 in table2
where (t1.TB1Key1 == t2.TB2Key1 || t1.TB1Key2 == t2.TB2Key2)
select new { t1, t2 }