0

I would need some guidance to convert this query in LINQ (method-based or expression syntax):

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.

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;
JPG
  • 545
  • 3
  • 19
  • Possible duplicate of [LINQ to SQL: Multiple joins ON multiple Columns. Is this possible?](https://stackoverflow.com/questions/5307731/linq-to-sql-multiple-joins-on-multiple-columns-is-this-possible) – JustLearning Jun 22 '18 at 08:34
  • check this out : https://stackoverflow.com/questions/5307731/linq-to-sql-multiple-joins-on-multiple-columns-is-this-possible – JustLearning Jun 22 '18 at 08:34

2 Answers2

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:

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 ...
Tomas Chabada
  • 2,869
  • 1
  • 17
  • 18
0

you code has wrong third condition instead of

join t3 in context.table3 on t3.fieldA equals t3.fieldC

put

join t3 in context.table3 on t2.fieldC equals t3.fieldA