LINQ | Join (Inner Join)

06 Jun, 2019
In LINQ, Join operators are used for integrating two data source into one data source which shares some common attributes. For example, in a bank, the manager has two lists, the first list contains the personal details and another list contains the home loan details. Now the manager wants to create a list which contains the names of those peoples who take a home loan and belong to the same place, so he uses join clause to create such type of list.The join clause always takes two data source, the elements present the data source must contain some property so that it can compare with other data source. The result of the join clause depends upon which type of join clause is used. The most common types of the join are:
Inner JoinCross JoinLeft outer joinGroup join
Inner Join
Cross Join
Left outer join
Group join
In LINQ, an inner join is used to serve a result which contains only those elements from the first data source that appears only one time in the second data source. And if an element of the first data source does not have matching elements, then it will not appear in the result data set. Here Join and Inner join are the same.
Important Points:
All the joins perform by join clause are equijoins.
It support query syntax in both C# and VB.Net languages.Syntax:join ... in ... 
on ... equals ...

Syntax:
join ... in ... 
on ... equals ...

It support method syntax in both C# and VB.Net languages. Here, the join method is overloaded in two different ways:Join <TOuter,TInner,TKey,TResult>(IEnumerable <TOuter>, IEnumerable <TInner>, Func <TOuter,TKey>,Func <TInner,TKey>,Func <TOuter,TInner,TResult>): This method allows you to correlate the elements of two sequences based on the given matching keys. Here, the default equality comparer is used to compare keys.Join<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>,IEnumerable<TInner>, Func<TOuter,TKey>,Func<TInner,TKey>,Func<TOuter,TInner,TResult>,IEqualityComparer<TKey>): This method allows you to correlate the elements of two sequences based on matching keys. A specified IEqualityComparer<T> is used to compare keys.
Join <TOuter,TInner,TKey,TResult>(IEnumerable <TOuter>, IEnumerable <TInner>, Func <TOuter,TKey>,Func <TInner,TKey>,Func <TOuter,TInner,TResult>): This method allows you to correlate the elements of two sequences based on the given matching keys. Here, the default equality comparer is used to compare keys.Join<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>,IEnumerable<TInner>, Func<TOuter,TKey>,Func<TInner,TKey>,Func<TOuter,TInner,TResult>,IEqualityComparer<TKey>): This method allows you to correlate the elements of two sequences based on matching keys. A specified IEqualityComparer<T> is used to compare keys.
Join <TOuter,TInner,TKey,TResult>(IEnumerable <TOuter>, IEnumerable <TInner>, Func <TOuter,TKey>,Func <TInner,TKey>,Func <TOuter,TInner,TResult>): This method allows you to correlate the elements of two sequences based on the given matching keys. Here, the default equality comparer is used to compare keys.
Join<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>,IEnumerable<TInner>, Func<TOuter,TKey>,Func<TInner,TKey>,Func<TOuter,TInner,TResult>,IEqualityComparer<TKey>): This method allows you to correlate the elements of two sequences based on matching keys. A specified IEqualityComparer<T> is used to compare keys.
Join method present in both the Queryable and Enumerable class.
The working of Join is similar like an inner join in SQL.
The join clause compares the data sources, according to specified keys using equals keyword, here == is not valid.
Example 1:
// C# program to illustrate the concept// of inner join in Query Syntaxusing System;using System.Linq;using System.Collections.Generic;  // Employee detailspublic class Employee1 {      public int emp_id    {        get;        set;    }      public string emp_name    {        get;        set;    }    public string emp_lang    {        get;        set;    }}  // Employee department detailspublic class Employee2 {      public int emp_id    {        get;        set;    }      public string emp_dept    {        get;        set;    }    public int emp_salary    {        get;        set;    }}  class GFG {      // Main method    static public void Main()    {        List<Employee1> emp1 = new List<Employee1>() {              new Employee1() {emp_id = 300, emp_name = "Anu",                                           emp_lang = "C#"},              new Employee1() {emp_id = 301, emp_name = "Mohit",                                              emp_lang = "C"},              new Employee1() {emp_id = 302, emp_name = "Sona",                                          emp_lang = "Java"},                                                        new Employee1() {emp_id = 303, emp_name = "Lana",                                          emp_lang = "Java"},                                                        new Employee1() {emp_id = 304, emp_name = "Lion",                                            emp_lang = "C#"},                                                          new Employee1() {emp_id = 305, emp_name = "Ramona",                                             emp_lang = "Java"},          };          List<Employee2> emp2 = new List<Employee2>() {              new Employee2() {emp_id = 300, emp_dept = "Designing",                                              emp_salary = 23000},              new Employee2() {emp_id = 301, emp_dept = "Developing",                                               emp_salary = 40000},              new Employee2() {emp_id = 302, emp_dept = "HR",                                       emp_salary = 50000},              new Employee2() {emp_id = 303, emp_dept = "Designing",                                              emp_salary = 60000},          };          // Query to find the name and        // the salary of the employees        // Using Inner Join        var res = from e1 in emp1                    join e2 in emp2                        on e1.emp_id equals e2.emp_id                            select new                             {                                Emp_Name = e1.emp_name,                                Emp_Salary = e2.emp_salary                            };          // Display result        Console.WriteLine("Employee and their Salary: ");        foreach(var val in res)        {            Console.WriteLine("Employee Name: {0} Salary: {1}",                                 val.Emp_Name, val.Emp_Salary);        }    }}
Employee and their Salary: 
Employee Name: Anu Salary: 23000
Employee Name: Mohit Salary: 40000
Employee Name: Sona Salary: 50000
Employee Name: Lana Salary: 60000

Example 2:
// C# program to illustrate the concept// of inner join in Method Syntaxusing System;using System.Linq;using System.Collections.Generic;  // Employee detailspublic class Employee1 {      public int emp_id    {        get;        set;    }      public string emp_name    {        get;        set;    }    public string emp_lang    {        get;        set;    }}  // Employee department detailspublic class Employee2 {      public int emp_id    {        get;        set;    }      public string emp_dept    {        get;        set;    }    public int emp_salary    {        get;        set;    }}  class GFG {      // Main method    static public void Main()    {        List<Employee1> emp1 = new List<Employee1>() {              new Employee1() {emp_id = 300, emp_name = "Anu",                                           emp_lang = "C#"},              new Employee1() {emp_id = 301, emp_name = "Mohit",                                              emp_lang = "C"},              new Employee1() {emp_id = 302, emp_name = "Sona",                                          emp_lang = "Java"},                                          new Employee1() {emp_id = 303, emp_name = "Lana",                                          emp_lang = "Java"},                                                        new Employee1() {emp_id = 304, emp_name = "Lion",                                            emp_lang = "C#"},                                                          new Employee1() {emp_id = 305, emp_name = "Ramona",                                            emp_lang = "Java"},          };          List<Employee2> emp2 = new List<Employee2>() {              new Employee2() {emp_id = 300, emp_dept = "Designing",                                              emp_salary = 23000},              new Employee2() {emp_id = 301, emp_dept = "Developing",                                               emp_salary = 40000},              new Employee2() {emp_id = 302, emp_dept = "HR",                                       emp_salary = 50000},              new Employee2() {emp_id = 303, emp_dept = "Designing",                                              emp_salary = 60000},          };          // Query to find the name and        // the department of the employees        // Using Join Method        var res = emp1.Join(emp2,                            e1 => e1.emp_id,                            e2 => e2.emp_id,                            (e1, e2) => new {                                EmployeeName = e1.emp_name,                                EmployeeDepartment = e2.emp_dept });          // Display result        Console.WriteLine("Employee Name and their Department:");        foreach(var val in res)        {            Console.WriteLine("Employee Name: {0} Department: {1}",                         val.EmployeeName, val.EmployeeDepartment);        }    }}
Employee Name and their Department:
Employee Name: Anu Department: Designing
Employee Name: Mohit Department: Developing
Employee Name: Sona Department: HR
Employee Name: Lana Department: Designing

CSharp LINQ
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# Dictionary with examples
C# | Multiple inheritance using interfaces
Introduction to .NET Framework
C# | Delegates
Differences Between .NET Core and .NET Framework
C# | Data Types
C# | Method Overriding
C# | String.IndexOf( ) Method | Set - 1
C# | Constructors
C# | Class and Object