1

How do I left join 2 Data Tables with Join based on multiple columns ?

I am using C#.

Found some solution here: Left Outer on two columns LINQ but getting compile time error.

dtblLeft:

id   col1   col2      
 1    1      S1
 2    1      any2
 3    2      S2
 4    3      S3
 5    3      any2
 6    5      any2
 7           any2

dtblRight:

col1   col2       Result
 1      S1        ConfigValue1
 2      S2        ConfigValue12
 3      S3        ConfigValue13
 4      S4        ConfigValue14

Need to do a left join on col1 and col2 values.

Result

id   col1   col2     Result 
 1    1      S1      ConfigValue1
 2    1      any2    
 3    2      S2      ConfigValue2
 4    3      S3      ConfigValue3
 5    3      any2
 6    5      any2
 7           any2

The answer suggested How to do joins in LINQ on multiple fields in single join:

var result = from x in entity
             join y in entity2
             on new { x.field1, x.field2 } equals new { y.field1, y.field2 }

if I use it with DataTable like

var result = from x in entity
             join y in entity2
             on new { x[field1], x[field2] } equals new { y[field1], y[field2] }

gives compilation error.

Community
  • 1
  • 1
Cannon
  • 2,725
  • 10
  • 45
  • 86
  • Possible answer here http://stackoverflow.com/questions/345427/linq-to-sql-join-multiple-columns-from-the-same-table or here http://stackoverflow.com/questions/373541/how-to-do-joins-in-linq-on-multiple-fields-in-single-join – Steve Jan 07 '14 at 23:24
  • Or here: http://stackoverflow.com/questions/373541/how-to-do-joins-in-linq-on-multiple-fields-in-single-join – Magnus Jan 07 '14 at 23:26
  • None of those answers seems to a solution for Left Join using multiple columns on a Data Tables. They expect object I believe. – Cannon Jan 07 '14 at 23:46

0 Answers0