如何做linq加入多个条件+ OR

时间:2013-02-14 21:14:57

标签: c# linq

我想知道如何使用多个条件和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()

ORs部分正在爆炸。

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 - REQUIRED JOIN

然后

T1.Col2必须匹配T2.Col2,除非T2.COl2 =“XYZ”然后才加入Col1

1 个答案:

答案 0 :(得分:1)

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

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")