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

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

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: