LINQ | Join (Inner Join)
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 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.
- All the joins perform by join clause are equijoins.
- It support query syntax in both C# and VB.Net languages.
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 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.
Employee and their Salary: Employee Name: Anu Salary: 23000 Employee Name: Mohit Salary: 40000 Employee Name: Sona Salary: 50000 Employee Name: Lana Salary: 60000
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