In the blog, I am writing about Group Join.
1. Group Join - Group Join produces hierarchical data structures. Each element from the first collection is paired with a set of correlated elements from the second collection.
2. When we prefer Group Join with Extension method, the syntax used is GroupJoin() extension method.
3. Open Visual Studio and create a New Project console Application.
4. Add two Class files, name it as Department.cs and Employee.cs.
5. In the Department.cs, write the code, given below.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication64 {
- public class Department {
- public int DeptId {
- get;
- set;
- }
- public string DeptName {
- get;
- set;
- }
- public static List < Department > GetAllDepartments() {
- return new List < Department > () {
- new Department {
- DeptId = 1, DeptName = "IT"
- },
- new Department {
- DeptId = 2, DeptName = "Accounting"
- },
- new Department {
- DeptId = 3, DeptName = "HR"
- },
- };
- }
- }
- }
6. It has two properties DeptId, DeptName and there is a Static Method GetAllDepartments(), which returns the list of Department object. It has three departments, which are IT, Accounting and HR.
7. The code written in Employee class is given below.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication64 {
- public class Employee {
- public int Id {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- public int DepartmentId {
- get;
- set;
- }
- public static List < Employee > GetAllEmployeesByDepartment() {
- return new List < Employee > () {
- new Employee {
- Id = 1, Name = "Sachin", DepartmentId = 1
- },
- new Employee {
- Id = 2, Name = "Rahul", DepartmentId = 1
- },
- new Employee {
- Id = 3, Name = "Sourav", DepartmentId = 2
- },
- new Employee {
- Id = 4, Name = "Ricky", DepartmentId = 2
- },
- new Employee {
- Id = 5, Name = "Adam", DepartmentId = 2
- },
- new Employee {
- Id = 6, Name = "Brian", DepartmentId = 2
- },
- new Employee {
- Id = 7, Name = "Glenn", DepartmentId = 3
- },
- new Employee {
- Id = 8, Name = "Chris", DepartmentId = 1
- },
- new Employee {
- Id = 9, Name = "Anil", DepartmentId = 3
- },
- new Employee {
- Id = 10, Name = "Lionnel", DepartmentId = 3
- },
- new Employee {
- Id = 11, Name = "Christiano", DepartmentId = 3
- },
- };
- }
- }
- }

Join the conversation! Your thoughts help the community grow.