LINQ to SQL Inner Join

In LINQ to SQL, the inner join will return only the records or rows that match both tables based on defined conditions.

Syntax of LINQ to SQL Inner Join

Following is the syntax of using LINQ to SQL Inner Join to get data from multiple tables.

 

C# Code

 

var result = from ed in db.EmployeeDetails
             join d in db.Departments on ed.DeptId equals d.DeptId
             select new
             {
               Name = ed.EmpName,
               Department = d.DeptName
             };

VB.NET Code

 

Dim result = From ed In db.EmployeeDetails Join d In db.Departments On ed.DeptId Equals d.DeptId
Select New With {.Name = ed.EmpName, .Department = d.DeptName}

If you observe above syntax we joined “EmployeeDetails”, “Departments” tables to get required data by using inner joins.

Example of LINQ to SQL Inner Join

Before we start implementing LINQ to SQL inner join example, we need to create a database with required tables and map those tables to LINQ to SQL file (.dbml). If you don't know the process, don't worry check this link create and map database tables to LINQ to SQL file (.dbml).

 

Once we create and map required tables to the .dbml file now, we will show data in our application for that Right-click on the application à select Add à New Item à Select Web Form à Give name as Default.aspx and click OK button.

 

Now open the Default.aspx page and write the code as shown below.

 

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Show Employee Details in Gridview</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvDetails" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>

Now open the code behind the file and write the code as shown below.

 

C# Code

 

protected void Page_Load(object sender, EventArgs e)
{
  EmployeeDBDataContext db = new EmployeeDBDataContext();
  if (!Page.IsPostBack)
  {
    var result = from ed in db.EmployeeDetails
                 join d in db.Departments on ed.DeptId equals d.DeptId
                 where d.DeptName.Equals("software")
                 select new
                 {
                   Name = ed.EmpName,
                   Location = ed.Location,
                   Gender = ed.Gender,
                   Department = d.DeptName
                 };
    gvDetails.DataSource = result;
    gvDetails.DataBind();
  }
}

VB.NET Code

 

Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim db As New EmployeeDBDataContext()
If Not Page.IsPostBack Then
Dim result = From ed In db.EmployeeDetails Join d In db.Departments On ed.DeptId Equals d.DeptId Where d.DeptName.Equals("software")
Select New With {.Name = ed.EmpName, .Location = ed.Location, .Gender = ed.Gender, .Department = d.DeptName}
gvDetails.DataSource = result
gvDetails.DataBind()
End If
End Sub
End Class

If you observe the above example, we are trying to get data from the “EmployeeDetails” and “Departments” table using an inner join.

Result of LINQ to SQL Inner Join

Following is the result of the LINQ to SQL inner join example.

 

LINQ to SQL Inner Join Query with Example

 

This is how we can use LINQ to SQL inner join with multiple tables in c#, vb.net to get required data from various tables based on our requirements.