Joining DataTables using LINQ in C#

There are some articles already on the internet which deals in this area but personally they didn’t fit my requirement due to one reason or the other.

In order to proceed with this article, I will first create two DataTable and fill them up with some random data.

The DataTable would be like the following:

DataTable 1:

See code

dataset visualizer

DataTable 2:

Datatable code

Datatable

Now, we will use the below LINQ query to merge the DataTable in such a way that the output is as in the following:

Table

The code for the same is as follows:

Code

The simple query just finds the item from both the DataTables having the common “ID” in them and gets the same in a different DataTable all together.

The output screen is as follows:

DataGrid

Please feel free to download the attached project for more understanding of the concept.