Background
I have read 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
LINQ
Language-Integrated Query (LINQ) is a set of features introduced in Visual Studio 2008 that extends powerful query capabilities to the language syntax of C# and Visual Basic. Using LINQ you can manipulate the data similar to what 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,
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New Project" - "C#" - "Empty Project" (to avoid adding a master page).
- Provide the Project name such as "JoinDataTableUsingLINQ" or another as you wish and specify the location.
- Then right-click on Solution Explorer and select "Add New Item" then select Default.aspx page.
- Drag and drop three Grid views to bind the records after Joining the two data tables.
Now the Default.aspx source code will be as follows.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body style="background-color: Blue">
<h4 style="color: White">
Article by Vithal Wadje
</h4>
<form id="form1" runat="server">
<div>
<h4 style="color: White">
Product Table Records Before joining
</h4>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
<br />
<h4 style="color: White">
Tax Master Table Records Before joining
</h4>
<asp:GridView ID="GridView2" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
<br />
</div>
<h4 style="color: White">
Tax and Product Table Records after joining
</h4>
<asp:GridView ID="GridView3" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
<br />
</form>
</body>
</html>





Shehzad KhanPosted Jan 26, 2023, 9:42 AM
Hi Vithal Wadje Sir, How to optimize this query in C# only during high volume data...//joining Product and Tax DataTable var JoinResult = (from p in dt.AsEnumerable() join t in dtTax.AsEnumerable() on p.Field<int>("Tax Id") equals t.Field<int>("Tax Id") select new { ProductName = p.Field<string>("Product Name"), BrandName = p.Field<string>("Brand Name"), ProductCategory = t.Field<string>("Product Category"), TaxCharge = t.Field<int>("Charge") }).ToList();
Ranjitha NarayanPosted Aug 10, 2017, 5:38 AM
Nice article sir , thank you it helped me alot code is working fine :)
AmitPosted Nov 4, 2016, 4:51 AM
LINQResultToDataTable<T>(IEnumerable<T> Linqlist howto convert var to datatable. Downloaded project contains nothing
Vithal WadjePosted May 25, 2015, 3:09 PM
yes
Saber ShaikhPosted Dec 15, 2014, 7:36 AM
I want Reduce This Code so..
Vithal WadjePosted Dec 15, 2014, 7:22 AM
yes saber shaikh sir why not ,you can reduce it
Saber ShaikhPosted Dec 15, 2014, 6:04 AM
can we Reduce this code for Execute
Vithal WadjePosted Dec 14, 2014, 1:14 PM
thanks sir
Jitendra KumarPosted Dec 14, 2014, 12:11 PM
Nice article..