简体   繁体   English

多个联接和逻辑运算符

[英]Multiple joins and logical operators

I would need some guidance to convert this query in LINQ (method-based or expression syntax): 我需要一些指导以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;

Using expression syntax it seems logical operators are not supported in the join clause. 使用表达式语法,join子句似乎不支持逻辑运算符。

My failed attempt: 我失败的尝试:

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;

EF does not support multiple objects to be used for joining, instead, you can create object that will contain all properties you want to apply equals on: 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 ...

you code has wrong third condition instead of 您的代码具有错误的第三个条件,而不是

join t3 in context.table3 on t3.fieldA equals t3.fieldC 在t3.fieldA的context.table3中加入t3等于t3.fieldC

put

join t3 in context.table3 on t2.fieldC equals t3.fieldA 在t2.fieldC上的context.table3中加入t3等于t3.fieldA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM