Joins In LINQ

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.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace ConsoleApplication64 {  
  6.  public class Department {  
  7.   public int DeptId {  
  8.    get;  
  9.    set;  
  10.   }  
  11.   public string DeptName {  
  12.    get;  
  13.    set;  
  14.   }  
  15.   public static List < Department > GetAllDepartments() {  
  16.    return new List < Department > () {  
  17.     new Department {  
  18.      DeptId = 1, DeptName = "IT"  
  19.     },  
  20.     new Department {  
  21.      DeptId = 2, DeptName = "Accounting"  
  22.     },  
  23.     new Department {  
  24.      DeptId = 3, DeptName = "HR"  
  25.     },  
  26.    };  
  27.   }  
  28.  }  
  29. }  
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.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace ConsoleApplication64 {  
  6.  public class Employee {  
  7.   public int Id {  
  8.    get;  
  9.    set;  
  10.   }  
  11.   public string Name {  
  12.    get;  
  13.    set;  
  14.   }  
  15.   public int DepartmentId {  
  16.    get;  
  17.    set;  
  18.   }  
  19.   public static List < Employee > GetAllEmployeesByDepartment() {  
  20.    return new List < Employee > () {  
  21.     new Employee {  
  22.      Id = 1, Name = "Sachin", DepartmentId = 1  
  23.     },  
  24.     new Employee {  
  25.      Id = 2, Name = "Rahul", DepartmentId = 1  
  26.     },  
  27.     new Employee {  
  28.      Id = 3, Name = "Sourav", DepartmentId = 2  
  29.     },  
  30.     new Employee {  
  31.      Id = 4, Name = "Ricky", DepartmentId = 2  
  32.     },  
  33.     new Employee {  
  34.      Id = 5, Name = "Adam", DepartmentId = 2  
  35.     },  
  36.     new Employee {  
  37.      Id = 6, Name = "Brian", DepartmentId = 2  
  38.     },  
  39.     new Employee {  
  40.      Id = 7, Name = "Glenn", DepartmentId = 3  
  41.     },  
  42.     new Employee {  
  43.      Id = 8, Name = "Chris", DepartmentId = 1  
  44.     },  
  45.     new Employee {  
  46.      Id = 9, Name = "Anil", DepartmentId = 3  
  47.     },  
  48.     new Employee {  
  49.      Id = 10, Name = "Lionnel", DepartmentId = 3  
  50.     },  
  51.     new Employee {  
  52.      Id = 11, Name = "Christiano", DepartmentId = 3  
  53.     },  
  54.    };  
  55.   }  
  56.  }  
  57. }  
8. Employee Class contains three properties, which are Id and Name of employees and DepartmentId. Here, DepartmentId is the Join Key. If we have to check which Department the Employee belongs to, we take the DepartmentId property from Employee and match it with the DeptId property of Department.
 
9. Thus, within these two classes DepartmentId from Employee and DeptId from Department, which are the Join keys. Also, in Employee class there is a method GetAllEmployeesByDepartment(), which returns the list of the employee object. 
 
10. We are going to retrieve Employees by Department, using a Group Join, and print the Department Name and Name of Employees belonging to that Department.
 
11. GetAllDepartments is going to give us the list of Department objects. Now, we want to group join the list of Department with the list of Employee, so we are going to the use the GroupJoin() Extension method.
 
Write the code, given below in Main Class.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace ConsoleApplication64 {  
  6.  class Program {  
  7.   static void Main(string[] args) {  
  8.    var employeebydept = Department.GetAllDepartments().GroupJoin(Employee.GetAllEmployeesByDepartment(),  
  9.     d => d.DeptId, e => e.DepartmentId, (department, employees) =>  
  10.     new {  
  11.      Departments = department,  
  12.       Employees = employees  
  13.     });  
  14.   
  15.    foreach(var dept in employeebydept) {  
  16.     Console.WriteLine(dept.Departments.DeptName);  
  17.     foreach(var employee in dept.Employees) {  
  18.      Console.Write(" ");  
  19.      Console.WriteLine(employee.Name);  
  20.     }  
  21.    }  
  22.   }  
  23.  }  
12. Group Join has 4 parameters, where first parameter is inner and its type is IEnumerable<TInner>.
 
13. In this, we want to join Employees with Department. We have the list of Departments, so the sequence is the list of employees. List of Department is the outer sequence, which is group joined with the inner sequence, which is the List of Employees.
 
14. Second Parameter is outerKeySelector, which is Join key property. Here, we provide the Join key for the outer sequence, which is Department. Thus, I have passed DeptId as a second parameter and next parameter is the innerKeySelector. Here, we provide Join key for the inner sequence, which is Employee here. DepartmentId from Employee is passed as third parameter. The final parameter is the resultSelector, which provides what we want as a result of Group Join. We need the list of Department and Employee.
 
15. Finally, we have got the list of Employees by Department in the variable employeebydept and we have to just print it, using foreach loop.
  1. Console.Write(dept.Departments.DeptName);  
  2. This will print the Department Name and within each Department we are going to have Employee  
  3.   
  4. foreach(var employee in dept.Employees) {  
  5.  Console.Write(" ");  
  6.  Console.WriteLine(employee.Name);  
  7. }  
This is printing the employees belonging to a particular department.
 
Here, we have used an extension syntax, i.e. GroupJoin method.