In LINQ, a Join clause with an into expression is called 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 are found between the right and left collections, then the join clause will return an empty array. In LINQ, group join is equivalent to inner equi join except that the result elements are organized into groups.
In c# or vb.net applications, we can use the result of the LINQ GROUP JOIN clause as a subquery based on our requirements.
Syntax of LINQ Group Join
Following is the syntax of using LINQ Group Join to get the matching elements from given 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 the above syntax, we used a Join clause with an into expression to get matching elements from the collections.
Example of LINQ Group Join
The following is LINQ Group Join's example 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 the above example, we used the Join clause with an into expression, and we are grouping elements based on department name.
Result of LINQ Group Join Example
Following is the result of the 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.