Updated March 31, 2023
Introduction to LINQ Join
LINQ Join operator is used to join two or more data sources together based on the common field between them and retrieving the matched records from the collection based on the particular conditions. Thus, it is fetching the data from two or more data sources based on the similar properties that appear in the data sources. Thus, the functionality of the operator is similar to SQL Joins.
LINQ Join Types
LINQ Join operates on two or more data sources; in other words, you can say as inner collection and outer collection from those both collection it returns a new collection it contains the records/data from both collections.
Syntax of LINQ Join:
public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer,
IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector,
Func<TInner, TKey> innerKeySelector,
Func<TOuter, TInner, TResult> resultSelector);
public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer,
IEnumerable<TInner> inner,
Func<TOuter, TKey> outerKeySelector,
Func<TInner, TKey> innerKeySelector,
Func<TOuter, TInner, TResult> resultSelector,
IEqualityComparer<TKey> comparer);
The above syntax contains two overloaded methods to execute the join methods; it contains the comparer as an additional argument.
When working with LINQ Join, we must understand the five things.
- Outer data source.
- Inner data source.
- Outer key selector can able to see the common key in the outer data source.
- Inner key selector can able to see the common key in the inner data source.
- Result selector to retrieve the data into the resultant set.
In LINQ Join, there are several types; let’s see the types of joins with their syntax with some examples.
1. INNER Join
Inner join is used to retrieve the matching records from both the data sources based on the specific conditions, and the non-matching will be eliminated from the resultant set.
Syntax:
Var _innerJoin=dataSource_1.getRecords_1 ()
.join(dataSource_2.getRecords_2()
data1=>data1.ID,
data2=>data2.ID, (data1, data2) =>new
{
_name=data.Name,
....
}).ToList();
2. LEFT Join
Left Join retrieves the records from the left collection and only the matching records from the right collection.
Syntax:
Var Left_Join= dataSource_1.getRecords_1 ()
.GroupJoin(dataSource_2.getRecords_2(),
data1=>data1.d2_ID,
data2=>data2.ID, (data1, data2) =>new
{
data1,data2
}). SelectMany( x=>x.d2.DefaultEmpty(), (data1,data2)=>new
{
........
});
3. CROSS Join
Cross Join returns the Cartesian product of records from the collection.
Syntax:
Var Cross_Join=data1.records2()
. Join(data2.records2(),
D1=>true,
D2=>true,
(D1,D2)=>new
{
..
});
4. GROUP Join
Group Join works similar to the join operator, whereas Group Join retrieves the result in groups based on a specific group key. Group Join performs the joining of two collections based on key and groups by the matching key and finally returns the collection of grouped key and result.
Syntax:
Var Group_Join=data1.records1()
.GroupJoin(
data2.records2(),
D1=>d1.ID,
D2=>D2.d2ID,
(D1,D2)=>new{D1,D2}
);
Example of LINQ Join
Below is the example mentioned:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console_LinQJoins
{
class Program
{ //Employee class
public class EmployeeClass_Master
{
public int Emp_ID
{
get;
set;
}
public string Emp_Name
{
get;
set;
}
public string Emp_Qualification
{
get;
set;
}
public int Emp_DeptID
{ get; set; }
}
// Employee department class
public class DepartmentClass_Master
{
public int Dept_ID
{ get; set; }
public int Emp_ID
{ get; set; }
public string emp_DeptName
{ get; set; }
public int Emp_Salary
{ get; set; }
}
class Linq_Joins_ProgramClass
{
static public void Main()
{
List<EmployeeClass_Master> empDetails = new List<EmployeeClass_Master>() {
new EmployeeClass_Master() {Emp_ID = 1000, Emp_Name = "Richard",
Emp_Qualification = "MCA",Emp_DeptID=101},
new EmployeeClass_Master() {Emp_ID = 1005, Emp_Name = "Ricky",
Emp_Qualification = "B.E",Emp_DeptID=103},
new EmployeeClass_Master() {Emp_ID = 1001, Emp_Name = "Jack",
Emp_Qualification = "MCA",Emp_DeptID=101},
new EmployeeClass_Master() {Emp_ID = 1007, Emp_Name = "Ponting",
Emp_Qualification = "B.E",Emp_DeptID=102},
new EmployeeClass_Master() {Emp_ID = 1003, Emp_Name = "Paul",
Emp_Qualification = "M.Sc",Emp_DeptID=102},
new EmployeeClass_Master() {Emp_ID = 1004, Emp_Name = "Beeja",
Emp_Qualification = "B.E",Emp_DeptID=103},
};
List<DepartmentClass_Master> deptDetails = new List<DepartmentClass_Master>()
{
new DepartmentClass_Master() {Dept_ID=101,Emp_ID = 1000, emp_DeptName = "Graphical-Designing", Emp_Salary = 60000},
new DepartmentClass_Master() {Dept_ID=102,Emp_ID = 1001, emp_DeptName = "Development", Emp_Salary = 35000},
new DepartmentClass_Master() {Dept_ID=103,Emp_ID = 1002, emp_DeptName = "Admin", Emp_Salary = 45000},
};
//using Cross Join method
var res_CrossJoin = empDetails
.Join(deptDetails,
emp => true,
dept => true,
(emp, dept) => new
{
Name = emp.Emp_Name,
dName = dept.emp_DeptName
}
);
//using the Group join method
var res_GroupJoin = deptDetails.
GroupJoin(
empDetails,
dept => dept.Dept_ID,
emp => emp.Emp_DeptID,
(dept, emp) => new { dept, emp }
);
//displaying Resultant set of Group Join, Outer Foreach for every department
Console.WriteLine("\n\nLINQ Group Join");
Console.WriteLine("-----------------");
foreach (var item in res_GroupJoin)
{
Console.WriteLine("\n\tDepartment Name :" + item.dept.emp_DeptName+"\n");
//Inner Foreach loop for each employee of a department
foreach (var employee in item.emp)
{
Console.WriteLine("\t\tEmployeeID : " + employee.Emp_ID + " , Name : " + employee.Emp_Name);
}
}
// displaying the result of Cross Join
Console.WriteLine("\nLINQ Cross Join");
Console.WriteLine("---------------");
Console.WriteLine("\tEmployee Name\tDepartment Names\n");
Console.WriteLine("\t-------------\t----------------");
foreach (var data in res_CrossJoin)
{
Console.WriteLine("\n\t{0}\t\t{1}",
data.Name, data.dName);
}
Console.WriteLine("\t-----------\t--------------\n");
Console.ReadLine();
}
}
}
}
Group join combines with two or more data sources it links only when having the common keys exist in both the data sources. LINQ Group join is used to grouping the records based on the presence common key. Group join generates hierarchical data structures; each and every element of the first data source is paired with a second data source.
In this output, we can’t see the Employee Name “Paul” because this employee does not belong to any department.
Output:
In the above sample program, we used Group join and Cross join. In cross join, we need to map each element in the first collection with each element in the second collection, so cross join is nothing but it produces the Cartesian products of the collection. For example, in the above code, the first list of collections contains 6 elements, and the second collection contains 3 elements, so finally, the resultant collection will be (6*3) totally of 18 elements.
Conclusion
In this article, we well-read about LINQ Joins, which are used to return the collection of elements based on given conditions using joins. Using several types of join methods, we can easily retrieve our records in the desired way based on several criteria.
Recommended Articles
This is a guide to LINQ Join. Here we discuss the introduction along with the LINQ join types for a better understanding. You may also have a look at the following articles to learn more –