【问题标题】:Entity Framework - How do I join with an OR condition on several keys实体框架 - 如何在多个键上加入 OR 条件
【发布时间】:2010-10-05 07:38:33
【问题描述】:

在两列上连接表格很容易

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

【问题讨论】:

    标签: linq join entity-framework-4


    【解决方案1】:

    这个怎么样:

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

    【讨论】:

    • 感谢您的尝试,但我想要连接时的条件,您只需进行交叉连接并选择符合条件的行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多