多个联接和逻辑运算符

时间:2018-06-22 08:32:57

标签: c# linq

我需要一些指导以LINQ(基于方法或表达式语法)转换此查询:

SELECT * from table1 t1 
JOIN table2 t2 ON t1.fieldA = t2.fieldA and t1.fieldB = t2.fieldB 
JOIN table3 t3 ON t2.fieldC = t3.fieldA
WHERE 
    t3.Enabled=1 and 
    t2.Active = 1 and
    t1.Linked=1;

使用表达式语法,join子句似乎不支持逻辑运算符。

我的尝试失败:

var query = from t1 in context.table1
            join t2 in context.table2 on t1.fieldA equals t2.fieldB && t1.fieldB equals t2.fieldB
            join t3 in context.table3 on t3.fieldA equals t3.fieldC
            where 
                t1.Enabled == 1 && t2.Active == 1 && t3.Linked == 1;

2 个答案:

答案 0 :(得分:0)

EF不支持将多个对象用于连接,相反,您可以创建一个对象,该对象包含要在以下对象上应用equals的所有属性:

var query = from t1 in context.table1
            join t2 in context.table2 on new {t1.fieldA, t1.fieldB} equals new {t2.fieldA, t2.fieldB}
            join t3 in context.table3 on t3.fieldA equals t3.fieldC
            where 
                t1.Enabled == 1 && t2.Active == 1 && t3.Linked == 1 ...

答案 1 :(得分:0)

您的代码的第三个条件错误 代替

在t3.fieldA的context.table3中加入t3等于t3.fieldC

放入

在t2.fieldC的context.table3中加入t3等于t3.fieldA