Posts Tagged ‘Linq to Sql’

LINQ # 10 – Join with multiple columns of the same table using Linq


While using LINQ as Linq to Sql queries , We often come across a scenario where we have to join the same table for multiple columns in it.

Following example illustrates how we can achieve it in Linq :

We have two tables Table1 and Table2 with columns column1 and column2 and we  have to join the Table1 with Table2 for both the columns.

We can achieve it using AnonymousType introduced by Microsoft.

Following is the code used for it


Join with multiple columns
  1. var lstDetails=(from tbl1 in Table1
  2.                 join tbl2 in  Table2
  3.                 on new {tbl1.column1,tbl1.column2}
  4.                 equals new {tbl2.column1,tbl2.column2}
  5.                 select tbl1 );

Thanks,                                                                                                                                          Ganesh