2

我想知道如何使用多个条件和 OR 进行 linq 连接。

例子:

var i = (from d in context.Table1
         join b in context.Table2
         on new {r1 = d.col1, r2 = d.col2}
         equals new {r1 = b.col1, r2 = b.col2}
         || b.col3.ToLower() equals "xyz"
         into bd
         from k in bd.DefaultIfEmpty()

手术室部分正在爆炸。

SQL 示例:

SELECT * FROM Table1 T1
LEFT JOIN Table2 T2 ON (T1.Col1 = T2.Col1 AND T1.Col2 =T2.Col2) 
                     OR (T1.Col1 = T2.Col1 AND T2.Col2 = 'XYZ')

解释:

T1.Col1 必须匹配 T2.COl1 - 需要加入

然后

T1.Col2 必须匹配 T2.Col2 除非 T2.COl2 = "XYZ" 然后只加入 Col1

4

1 回答 1

1

尝试将“join”移动到 ANSI 82 的“Where”子句中:

var i = (from d in context.Table1
         from b in context.Table2
         where (b == null)
               || (d.col1 = b.col1 && d.col2 == b.col2 ) 
               || (b.col3.ToLower() == "xyz")
于 2013-02-14T21:40:39.047 回答