左边用OR条件加入

时间:2013-10-27 19:52:47

标签: c# entity-framework join sql-to-linq-conversion

如何在实体框架中表示以下LEFT JOIN

SELECT * FROM a 
LEFT JOIN b ON 
(a.UserId = b.Field1 AND b.Field2 = SOME_VARIABLE) 
OR (a.UserId = b.Field2 AND b.Field1 = SOME_VARIABLE)

对于AND我知道我可以使用复杂的匿名类型和EQUALS,但我无法弄清楚如何处理OR

1 个答案:

答案 0 :(得分:2)

var query = from a in context.aTable
            select new {
               A = a,
               B = (from b in context.bTable
                     where (a.UserId == b.Field1 && b.Field2 == SOME_VARIABLE) ||
                           (a.UserId == b.Field2 && b.Field1 == SOME_VARIABLE)
                    select b
                   ).DefaultIfEmpty()
            }

您可以根据需要忽略DefaultIfEmpty