LINQ Group Join

  Total Views : 171
  Comments : 0
 
  Keywords : Linq groupjoin with example in c# / vb.net, Linq group join to produce a sequence of elements based on matching elements from the collection in c# / vb.net, How to use linq group join in c#

Here we will learn LINQ group join in c#, vb.net with example and how to use LINQ group join to get matching elements from the left collection based on conditions in c#, vb.net with example.

LINQ Group Join

In LINQ a Join clause with an into expression is called as a Group join. In LINQ Group join produces a sequence of object elements based on the matching elements from both left and right collections.

 

In case If no matching elements found from right collection with left collection then the join clause will return an empty array. This LINQ Group join is an equivalent to inner equi join except that the result elements organized into groups.

 

In applications we can use the result of GROUP JOIN clause as a sub query.

Syntax of LINQ Group Join

Following is the syntax of using LINQ Group Join to get matching elements from collections based on our requirements.

 

C# Code

 

var result = from d in objDept

join e in objEmp on d.DepId equals e.DeptId into empDept

select new

{

DepartmentName = d.DepName,

Employees = from emp2 in empDept

orderby emp2.Name

select emp2

};

VB.NET Code

 

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

Select New With {.DepartmentName = d.DepName, .Employees = From emp2 In empDept Order By emp2.Name Select emp2}

If you observe above syntax we used Join clause with an into expression to get matching elements from the collections.

Example of LINQ Group Join

Following is the example of LINQ Group Join to get matching elements from the collections based on required 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 Kumar", DeptId=2 },

new Employee { EmpId=4,Name = "Sateesh Chandra", 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 into empDept

select new

{

DepartmentName = d.DepName,

Employees = from emp2 in empDept

orderby emp2.Name

select emp2

};

int totalItems = 0;

foreach (var empGroup in result)

{

Console.WriteLine(empGroup.DepartmentName);

foreach (var item in empGroup.Employees)

{

totalItems++;

Console.WriteLine("    {0}", item.Name);

}

}

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 Kumar", .DeptId = 2},

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

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

}

 

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

Select New With {.DepartmentName = d.DepName, .Employees = From emp2 In empDept Order By emp2.Name Select emp2}

For Each empGroup In result

Console.WriteLine(empGroup.DepartmentName)

For Each item In empGroup.Employees

Console.WriteLine("    {0}", item.Name)

Next

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 used Join clause with an into expression and we are grouping elements based on department name.

Output of LINQ Group Join Example

Following is the result of LINQ Group Join example.

 

Software

 

    Rohini Alavala

    Suresh Dasari

 

Finance

 

    Praveen Kumar

    Sateesh Chandra

 

Health

This is how we can use LINQ Group Join to get matching elements from collections.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User