Back to: LINQ Tutorial For Beginners and Professionals
LINQ Joins in C#
In this article, I am going to give you an overview of LINQ Joins in C#. Please read our previous article where we discussed the LINQ ToLookup Method in C# with Examples. As part of this article, we are going to discuss the following pointers
- What are LINQ Join Operations?
- Why do we need to perform the Join Operations?
- What are the different methods available to perform Join Operations in LINQ?
- Finally, what are the different types of LINQ Joins?
If you have any experience in database systems like SQL Server, Oracle, MySQL, etc. then you may have familiar with SQL Joins. The LINQ Joins are not different. They are also used to merge the data from two or more data sources (tables or objects) into a single result set based on some common property. Using LINQ Joins, it is also possible to fetch the data based on some conditions.
What are LINQ Joins?
As per the Microsoft documentation, The joining of two data sources is the association of objects in one data source with objects that share a common attribute in another data source.
We can simplify the above definition as Join operations are used to fetch the data from two or more data sources based on some common properties present in the data sources.
Why do we need to perform Join Operations?
Let us understand why we need to perform Join Operations with an example. For example, let’s say we have the following three data sources (Employee, Department, and Address).
Now we need to fetch the data from the above three data sources as shown below.
As you can see the above result set contains the data from all three data sources. The most important point that you need to understand is while performing join you need the common property. In this case, the common property between Employee and Department is Department Id which is present in both the data sources. In the same line, Address ID is the common property between the Employee and Address data sources. So scenarios like this we need to use LINQ Join to fetch the data. How to write the query to fetch the data we will discuss in our next article.
What are the Methods Available in LINQ to Perform the Join Operations?
There are two methods available in LINQ to perform Join Operations. They are as follows:
- Join: This operator is used to join two data sources or collections based on common property and return the data as a single result set.
- GroupJoin: This operator is also used to join two data sources or collections based on a common key or property but returns the result as a group of sequences.
What are the different Types of LINQ Joins Available in C#?
We can perform different types of joins such as Inner Join, Left Join, Right Join, Full Join, and Cross Join in LINQ. For a better understanding, please have a look at the below image.
Inner Join: The Inner Join is used to return only the matching rows from both the data sources involved in the join by removing the non-matching records.
Left Outer Join: The LEFT OUTER JOIN is used to retrieve all the matching rows from both the data sources involved in the join as well as non-matching rows from the left side data source. In this case, the un-matching data will take a null value.
Right Outer Join: The RIGHT OUTER JOIN is used to retrieve all the matching rows from both the data sources involved in the join as well as non-matching rows from the right-side data source. In this case, the un-matching data will take NULL values.
Full Outer Join: The Full Outer Join is used to retrieve all the matching rows as well as all the non-matching rows from both the data sources involved in the JOIN. The non-matching data in such cases will take the NULL values.
Cross Join: In Cross Join, each record of a data source is joined with each record of the other data source.
In the next article, I am going to discuss the LINQ Inner Join in C# with Examples. In this article, I try to give an overview of LINQ Join in C# and I hope you enjoy this article.