In this Video, I am going to discuss the LINQ Intersect Method in C# using examples. Please read our previous Video where we discussed the LINQ Except() Method with examples. As part of this Video, I am going to discuss the following pointers

- What is LINQ Intersect Method?
- Examples of LINQ Intersect Method using both Method and Query Syntax
- How to implement IEqualityComparer?
LINQ Intersect Method in C#:
The LINQ Intersect Method in C# is used to return the common elements from both the collections. The elements that are present in both the data sources. There are two overloaded versions available for the LINQ Intersect Method as shown below.
The one and the only difference between the above two LINQ Intersect methods is that the second overloaded version takes IEqualityComparer as an argument. That means the Intersect Method is also used for Comparer.
Let us understand this with an example:
As shown in the above image, here we have two integer data sources i.e. DataSource 1 and Data Source 2. The DataSource 1 contains elements such as 1, 2, 3, 4, 5, 6 and the DataSource 2 contains elements such as 1, 3, 5, 8, 9, and 10. If we want to retrieve the elements such as 1, 3, and 5 which are exist in both the data sources then we need to use the LINQ Intersect method.
Example1:
The following example shows the use of the LINQ Intersect() Method using both Method and Query Syntax to fetch the common elements exists in both the collections.
Run the application and you will see the output as expected.
Note: In query syntax, there is no such operator call Intersect, so here we need to use the mixed syntax i.e. both the query and method syntax to achieve the same.
Example2:
Here we have two arrays of countries and we need to return the common countries from both the collections.
Now run the application and have a look at the output as shown below.
As you can see it displays only India and Canada. If you look at our collections, then you can see the country “UK” is present in both the collections but the Intersect method did not fetch that country. This is because the default comparer that is being used by the Intersect method is case-insensitive.
So if you want to ignore the case-sensitive then you need to use the other overloaded version of the Intersect() method which takes IEqualityComparer as an argument. So, modify the program as shown below where we pass StringComparer as an argument.
Now run the application and it will display the data as expected as shown below
.
LINQ Intersect() Method with Complex Type:
The LINQ Intersect() Method like other Set Operators (such as Distinct, Expect) also works in a different manner when working with complex types such as Product, Employee, Student, etc. Let us understand this with an example.
Create a class file with the name Student.cs and then copy and paste the following code in it.
This is a very simple student class with just two properties. Let say, we have the following two data sources.
As you can see in the above image, we have two collections of student data. And if you notice we have two students which are appeared in both the collections.
Example3:
Our requirement is to fetch all the student names which are present in both the collections. That is the common students names from both the collections.
Output:
Example4:
Now we need to select all the information of all the students those are present in both the collections. In order to do this, let us modify the program class as shown below.
Once you run the application, then it will not display any data. This is because the default comparer which is used for comparison is only checked whether two object references are equal and not the individual property values of the complex object.
Let us see how to use an anonymous type to solve the above problem.
Using Anonymous Type for Intersect Operation in C#:
In this approach, we need to select all the individual properties to an anonymous type. The following program does exactly the same things.
Now run the application and it should display the output as expected as shown below.
Let us see how to achieve the same thing using Comparer.
Using Comparer:
In this approach, we need to create a class and then we need to implement the IEqualityComparer interface. So, create a class file with the name StudentComparer.cs and then copy and paste the following code in it.
Now, we need to create an instance of StudentComparer class and then we need to pass that instance to the Intersect method as shown in the below program.