ASP.NET 5 Hosting :: How To Join Two DataTables Using LINQ In ASP.Net C#

Many forum posts regarding how to join two DataTables, so by considering those requirements I have decided to write this article. So let us learn step-by-step how to Join two DataTables using  LINQ


Language-Integrated Query (LINQ) is a set of features introduced in Visual Studio that extends powerful query capabilities to the language syntax of C# and Visual Basic. Using LINQ you can manipulate the data similar to as is done using SQL queries . Let us learn practically how to convert a LINQ query result into a DataTable by creating a sample application.
Now create the project as:
  1. “Start” – “All Programs” – “Microsoft Visual Studio 2012”.
  2. “File” – “New Project” – “C#” – “Empty Project” (to avoid adding a master page).
  3. Provide the Project name such as “JoinDataTableUsingLINQ” or another as you wish and specify the location.
  4. Then right-click on Solution Explorer and select “Add New Item” then select Default.aspx page.
  5. Drag and drop three Grid view to bind the records after Joining the two data table .

Now the Default.aspx source code will be as follows:

Now let’s use the scenario that we have one Product table and one Tax Master Table and we want to join these tables depending on Tax Id, so let us create the tables instead of going to the DataBase.
Open the Default.aspx.cs Page and provide the following code to create the Product table as in the following:

The Product table records will be such as follows:


Now let us create the Tax Master table using the following code:

Now  the Tax Master table records will be such as follows:


Now let us see the preceding two tables, one is Product and the other is Tax Master. From them we want to display the tax charge of each product and tax charges are in the Tax Master table, so to get it we need to join the preceding two tables using LINQ .
So let us write the code to join the Product and Tax Master tables as in the following:

Now create a function to name the LINQResultToDataTable that converts the LINQ result to a table as in the following:

The preceding function takes the LINQ query result and converts it into the Data Table.

Now we have a common DataTable after joining the Product and Tax Master tables, so let us bind three grid views from the three Data Tables so we can understand the difference. The entire code of Defualt.aspx.cs will be such as follows.


  • Download the Zip file from the attachment for the full source code of the application.
  • You can also bind the DataTables from the database.


I hope this article is useful for all readers, if you have any suggestion then please contact me including beginners also.

Leave a Reply

Your email address will not be published.