Part - 13 LINQ Intersect Method
Part - 13 LINQ Intersect Method
The LINQ Intersect Method in C# is used to return the common elements from both collections. The
elements that are present in both data sources are going to be returned by Intersect Method. There
are two overloaded versions available for the Intersect Method in C#. They are as follows.
The one and only difference between the above two LINQ Intersect methods is that the second
overloaded version takes IEqualityComparer as an argument. That means when we are working with
Complex Types, in order to work as expected, we can use the overloaded method which takes the
IEqualityComparer parameter.
//Method Syntax
var MS = dataSource1.Intersect(dataSource2).ToList();
//Query Syntax
var QS = (from num in dataSource1
select num)
.Intersect(dataSource2).ToList();
Console.ReadKey();
}
}
}
//Method Syntax
var MS = dataSource1.Intersect(dataSource2).ToList();
//Query Syntax
var QS = (from country in dataSource1
select country)
.Intersect(dataSource2).ToList();
Console.ReadKey();
}
}
}
Now run the application and you should get the output as expected as shown in the below image.
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 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 to the Intersect() method which will ignore the
case sensitivity while comparing the values.
using System;
using System.Linq;
namespace LINQDemo
{
class Program
{
static void Main(string[] args)
{
string[] dataSource1 = { "India", "USA", "UK", "Canada", "Srilanka" };
string[] dataSource2 = { "India", "uk", "Canada", "France", "Japan" };
//Method Syntax
var MS = dataSource1.Intersect(dataSource2,
StringComparer.OrdinalIgnoreCase).ToList();
//Query Syntax
var QS = (from country in dataSource1
select country)
.Intersect(dataSource2, StringComparer.OrdinalIgnoreCase).ToList();
Console.ReadKey();
}
}
}
Now run the application and it will display the data as expected as shown in the below image.
//Method Syntax
var MS = StudentCollection1.Select(x => x.Name)
.Intersect(StudentCollection2.Select(y => y.Name)).ToList();
//Query Syntax
var QS = (from std in StudentCollection1
select std.Name)
.Intersect(StudentCollection2.Select(y => y.Name)).ToList();
Console.ReadKey();
}
}
}
When you run the above code, you will get the output as expected as shown in the below image:
//Method Syntax
var MS = StudentCollection1.Intersect(StudentCollection2).ToList();
//Query Syntax
var QS = (from std in StudentCollection1
select std).Intersect(StudentCollection2).ToList();
Console.ReadKey();
}
}
}
Once you run the application, then it will not display any data. This is because the default comparer
which is used by the LINQ Intersect Method for comparison is only checked whether two object
references are equal and not the individual property values of the complex object. And in this case,
each student object has a different reference and hence it is not returning any student data. As we
already discussed we can overcome this problem in many different ways. Let us understand each way
one by one.
//Method Syntax
var MS = StudentCollection1
.Intersect(StudentCollection2, studentComparer).ToList();
//Query Syntax
var QS = (from std in StudentCollection1
select std)
.Intersect(StudentCollection2, studentComparer).ToList();
Console.ReadKey();
}
}
}
With the above changes in place, run the application and you will get the output as expected as
shown in the below image.
//Method Syntax
var MS = StudentCollection1.Select(x => new { x.ID, x.Name })
.Intersect(StudentCollection2.Select(x => new { x.ID, x.Name })).ToList();
//Query Syntax
var QS = (from std in StudentCollection1
select new { std.ID, std.Name })
.Intersect(StudentCollection2.Select(x => new { x.ID, x.Name })).ToList();
foreach (var student in MS)
{
Console.WriteLine($" ID : {student.ID} Name : {student.Name}");
}
Console.ReadKey();
}
}
}
With the above changes in place, now run the application code again and you will get the output as
expected as shown in the below image.
//Method Syntax
var MS = StudentCollection1.Intersect(StudentCollection2).ToList();
//Query Syntax
var QS = (from std in StudentCollection1
select std).Intersect(StudentCollection2).ToList();
Console.ReadKey();
}
}
}
Now run the application and again, you will get the same output as the previous two examples as
shown in the below image.
//Method Syntax
var MS = StudentCollection1.Intersect(StudentCollection2).ToList();
//Query Syntax
var QS = (from std in StudentCollection1
select std).Intersect(StudentCollection2).ToList();
Console.ReadKey();
}
}
}
Now, run the application and you will also get the same output as expected.