A LINQ JOIN keyword is used to combine rows from two or more tables, based on a common field between them. Like SQL Joins, the Linq is also provided some keywords to achieve Inner Join and Outer Join. As we know from SQL outer join is divided into 2 groups that is Left Outer Join and Right Outer Join. Here, in this tip, we learn how to achieve these joins in LINQ. Let us discuss what is inner join.
  • INNER JOIN: Inner Join only return matched records between two or more tables based on a common field between these tables.

  • LEFT OUTER JOIN: Return all rows from the left table, and the matched rows from the right table.

  • RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table.

  • FULL JOIN: Return all rows when there is a match in ONE of the tables.
Here in this console application, I have two lists (EmployeeList and DepartmentList). Considering these 2 lists as 2 different tables, I am joining these 2 tables on the basis of a common column, i.e., ID.
INNER JOIN
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Joins
  7. {
  8. public class Employee
  9. {
  10. public int Id { get; set; }
  11. public int age { get; set; }
  12. public string name { get; set; }
  13. public string gender { get; set; }
  14. }
  15. public class Department
  16. {
  17. public int id { get; set; }
  18. public string Departments { get; set; }
  19. public string Location { get; set; }
  20. }
  21. class Demo
  22. {
  23. static void Main(string[] args)
  24. {
  25. List<Employee> li = new List<Employee>();
  26. li.Add(new Employee
  27. { Id = 1, age = 19, name = "Ritesh", gender = "M" });
  28. li.Add(new Employee
  29. { Id = 2, age = 20, name = "sujit", gender = "M" });
  30. li.Add(new Employee
  31. { Id = 3, age = 23, name = "Kabir", gender = "F" });
  32. li.Add(new Employee
  33. { Id = 4, age = 3, name = "mantu", gender = "F" });
  34. li.Add(new Employee
  35. { Id = 5, age = 24, name = "Kamlesh", gender = "M" });
  36. li.Add(new Employee
  37. { Id = 6, age = 28, name = "Manoj", gender = "M" });
  38. List<Department> Deli = new List<Department>();
  39. Deli.Add(new Department
  40. { id = 2, Departments = "IT", Location = "Bangalore" });
  41. Deli.Add(new Department
  42. { id = 8, Departments = "IT", Location = "Bangalore" });
  43. Deli.Add(new Department
  44. { id = 3, Departments = "HR", Location = "Bangalore" });
  45. Deli.Add(new Department
  46. { id = 7, Departments = "HR", Location = "Bangalore" });
  47. Deli.Add(new Department
  48. { id = 6, Departments = "Account", Location = "Bangalore" });
  49. var result = from emp in li
  50. join
  51. de in Deli
  52. on emp.Id equals de.id
  53. select new
  54. {
  55. EmployeeId = emp.Id,
  56. EmployeeName = emp.name,
  57. Department = de.Departments,
  58. Location = de.Location
  59. };
  60. Console.WriteLine(" ID\t\tName\t\t DepartmentName \t\tLocation");
  61. foreach (var obj in result)
  62. {
  63. Console.WriteLine(obj.EmployeeId + "\t\t" + obj.EmployeeName +
  64. "\t\t" + obj.Department + "\t\t\t\t" + obj.Location);
  65. }
  66. Console.ReadLine();
  67. }
  68. }
  69. }
Now here is the result produced.
LEFT OUTER JOIN
Here is the program for Left Outer join.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Joins
  7. {
  8. public class Employee
  9. {
  10. public int Id { get; set; }
  11. public int age { get; set; }
  12. public string name { get; set; }
  13. public string gender { get; set; }
  14. }
  15. public class Department
  16. {
  17. public int id { get; set; }
  18. public string Departments { get; set; }
  19. public string Location { get; set; }
  20. }
  21. class Demo
  22. {
  23. static void Main(string[] args)
  24. {
  25. List<Employee> li = new List<Employee>();
  26. li.Add(new Employee { Id = 1, age = 19, name = "Ritesh", gender = "M" });
  27. li.Add(new Employee { Id = 2, age = 20, name = "sujit", gender = "M" });
  28. li.Add(new Employee { Id = 3, age = 23, name = "Kabir", gender = "F" });
  29. li.Add(new Employee { Id = 4, age = 3, name = "mantu", gender = "F" });
  30. li.Add(new Employee { Id = 5, age = 24, name = "Kamlesh", gender = "M" });
  31. li.Add(new Employee { Id = 6, age = 28, name = "Manoj", gender = "M" });
  32. List<Department> Deli = new List<Department>();
  33. Deli.Add(new Department { id = 2, Departments = "IT", Location = "Bangalore" });
  34. Deli.Add(new Department { id = 8, Departments = "IT", Location = "Ranchi" });
  35. Deli.Add(new Department { id = 3, Departments = "HR", Location = "bihar" });
  36. Deli.Add(new Department { id = 7, Departments = "HR", Location = "bhubaneshwar" });
  37. Deli.Add(new Department { id = 6, Departments = "Account", Location = "keonjhar" });
  38. var result = from emp in li
  39. join
  40. de in Deli
  41. on emp.Id equals de.id into tempstorage
  42. from dx in tempstorage.DefaultIfEmpty()
  43. select new
  44. {
  45. EmployeeId = emp.Id,
  46. EmployeeName = emp.name,
  47. Department = (dx != null) ? dx.Departments : "NULL",
  48. Location = (dx != null) ? dx.Location : "NULL"
  49. };
  50. Console.WriteLine("ID\t\tName\t\t DepartmentName \t\tLocation");
  51. foreach (var obj in result)
  52. {
  53. Console.WriteLine(obj.EmployeeId + "\t\t" +
  54. obj.EmployeeName + "\t\t" + obj.Department + "\t\t\t\t" + obj.Location);
  55. }
  56. Console.ReadLine();
  57. }
  58. }
  59. }
The output produced can be seen below:
RIGHT OUTER JOIN
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Joins
  7. {
  8. public class Employee
  9. {
  10. public int Id { get; set; }
  11. public int age { get; set; }
  12. public string name { get; set; }
  13. public string gender { get; set; }
  14. }
  15. public class Department
  16. {
  17. public int id { get; set; }
  18. public string Departments { get; set; }
  19. public string Location { get; set; }
  20. }
  21. class Demo
  22. {
  23. static void Main(string[] args)
  24. {
  25. List<Employee> li = new List<Employee>();
  26. li.Add(new Employee
  27. { Id = 1, age = 19, name = "Ritesh", gender = "M" });
  28. li.Add(new Employee
  29. { Id = 2, age = 20, name = "sujit", gender = "M" });
  30. li.Add(new Employee
  31. { Id = 3, age = 23, name = "Kabir", gender = "F" });
  32. li.Add(new Employee
  33. { Id = 4, age = 3, name = "mantu", gender = "F" });
  34. li.Add(new Employee
  35. { Id = 5, age = 24, name = "Kamlesh", gender = "M" });
  36. li.Add(new Employee
  37. { Id = 6, age = 28, name = "Manoj", gender = "M" });
  38. List<Department> Deli = new List<Department>();
  39. Deli.Add(new Department
  40. { id = 2, Departments = "IT", Location = "Bangalore" });
  41. Deli.Add(new Department
  42. { id = 8, Departments = "IT", Location = "Bangalore" });
  43. Deli.Add(new Department
  44. { id = 3, Departments = "HR", Location = "Bangalore" });
  45. Deli.Add(new Department
  46. { id = 7, Departments = "HR", Location = "Bangalore" });
  47. Deli.Add(new Department
  48. { id = 6, Departments = "Account", Location = "Bangalore" });
  49. var result = from
  50. de in Deli
  51. join
  52. emp in li
  53. on de.id equals emp.Id into tempstorage
  54. from dx in tempstorage.DefaultIfEmpty()
  55. select new
  56. {
  57. EmployeeId = (dx != null) ? dx.Id :0,
  58. EmployeeName = (dx != null) ? dx.name : "NULL",
  59. Department = de.Departments,
  60. Location = de.Location
  61. };
  62. Console.WriteLine("ID\t\tName\t\t DepartmentName \t\tLocation");
  63. foreach (var obj in result)
  64. {
  65. Console.WriteLine(obj.EmployeeId + "\t\t" + obj.EmployeeName +
  66. "\t\t" + obj.Department + "\t\t\t\t" + obj.Location);
  67. }
  68. Console.ReadLine();
  69. }
  70. }
  71. }
So in this, we can see 2 different tables in different ways in LINQ.