LINQ Inner Join

  Total Views : 221
  Comments : 0
 
  Keywords : How to use linq inner join in c# / vb.net with example, Linq inner join query with example in c# / vb.net, Inner join in linq with multiple conditions example in c# / vb.net

Here we will learn LINQ inner join in c#, vb.net with example and how to use LINQ inner join to get matching elements from collection with defined conditions in c#, vb.net with example.

LINQ Inner Join

In LINQ Inner join is used to return only the matched records or elements from the collections based on specified conditions.

Syntax of LINQ Inner Join

Following is the syntax of using LINQ inner join to get elements from the collections based on specified conditions.

 

C# Code

 

var result = from d in objDept

join e in objEmp

on d.DepId equals e.DeptId

select new

{

EmployeeName = e.Name,

DepartmentName = d.DepName

};

VB.NET Code

 

Dim result = From d In objDept Join e In objEmp On d.DepId Equals e.DeptId

Select New With {.EmployeeName = e.Name, .DepartmentName = d.DepName}

If you observe above syntax we are trying to get elements from “objEmp”, “objDept” collections based on matching “DeptId” column values.

Example of LINQ Inner Join

Following is the example of using LINQ Inner join to get elements from collections based on specified conditions.

 

C# Code

 

using System;

using System.Collections.Generic;

using System.Linq;

 

namespace Linqtutorials

{

class Program

{

static void Main(string[] args)

{

List<Department> objDept = new List<Department>(){

new Department{DepId=1,DepName="Software"},

new Department{DepId=2,DepName="Finance"},

new Department{DepId=3,DepName="Health"}

};

List<Employee> objEmp = new List<Employee>()

{

new Employee { EmpId=1,Name = "Suresh Dasari", DeptId=1 },

new Employee { EmpId=2,Name = "Rohini Alavala", DeptId=1 },

new Employee { EmpId=3,Name = "Praveen Alavala", DeptId=2 },

new Employee { EmpId=4,Name = "Sateesh Alavala", DeptId =2},

new Employee { EmpId=5,Name = "Madhav Sai"}

};

var result = from d in objDept

join e in objEmp

on d.DepId equals e.DeptId

select new

{

EmployeeName = e.Name,

DepartmentName = d.DepName

};

foreach (var item in result)

{

Console.WriteLine(item.EmployeeName + "\t | " + item.DepartmentName);

}

Console.ReadLine();

}

}

class Department

{

public int DepId { get; set; }

public string DepName { get; set; }

}

class Employee

{

public int EmpId { get; set; }

public string Name { get; set; }

public int DeptId { get; set; }

}

}

VB.NET Code

 

Module Module1

Sub Main()

Dim objDept As New List(Of Department)() From {

New Department With {.DepId = 1, .DepName = "Software"},

New Department With {.DepId = 2, .DepName = "Finance"},

New Department With {.DepId = 3, .DepName = "Health"}

}

Dim objEmp As New List(Of Employee)() From {

New Employee With {.EmpId = 1, .Name = "Suresh Dasari", .DeptId = 1},

New Employee With {.EmpId = 2, .Name = "Rohini Alavala", .DeptId = 1},

New Employee With {.EmpId = 3, .Name = "Praveen Alavala", .DeptId = 2},

New Employee With {.EmpId = 4, .Name = "Sateesh Alavala", .DeptId = 2},

New Employee With {.EmpId = 5, .Name = "Madhav Sai"}

}

Dim result = From d In objDept Join e In objEmp On d.DepId Equals e.DeptId

Select New With {.EmployeeName = e.Name, .DepartmentName = d.DepName}

For Each item In result

Console.WriteLine(item.EmployeeName + vbTab & " | "  + item.DepartmentName)

Next

Console.ReadLine()

End Sub

Class Department

Public Property DepId() As Int32

Get

Return m_DepId

End Get

Set(ByVal value As Int32)

m_DepId = value

End Set

End Property

Private m_DepId As Int32

Public Property DepName() As String

Get

Return m_DepName

End Get

Set(ByVal value As String)

m_DepName = value

End Set

End Property

Private m_DepName As String

End Class

 

Class Employee

Public Property EmpId() As Int32

Get

Return m_EmpId

End Get

Set(ByVal value As Int32)

m_EmpId = value

End Set

End Property

Private m_EmpId As Int32

Public Property Name() As String

Get

Return m_Name

End Get

Set(ByVal value As String)

m_Name = value

End Set

End Property

Private m_Name As String

Public Property DeptId() As String

Get

Return m_DeptId

End Get

Set(ByVal value As String)

m_DeptId = value

End Set

End Property

Private m_DeptId As String

End Class

End Module

If you observe above example we are getting employee name and department name by from both the collections where employees mapped with department.

Output of LINQ Inner Join Example

Following is the result of LINQ Inner join example.

 

Suresh Dasari      | Software

Rohini Alavala     | Software

Praveen Alavala  | Finance

Sateesh Alavala  | Finance

This is how we can use LINQ Inner Join in c#, vb.net to get elements from collections based on requirements.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User