LINQ Intersect Method

LINQ Intersect Method with Examples in C#

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

  1. What is LINQ Intersect Method?
  2. Examples of LINQ Intersect Method using both Method and Query Syntax
  3. 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. That is the elements which are present in both the data sources. There are two overloaded versions available for the LINQ Intersect Method as shown below.

LINQ Intersect Method with Examples in C#

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:

LINQ Intersect Method in C#

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 exists in both the data sources then we need to use the LINQ Intersect method.

Example1:

The following example shows the use of LINQ Intersect() Method using both Method and Query Syntax to fetch the common elements exists in both the collections.

using System.Collections.Generic;
using System;
using System.Linq;
namespace LINQDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> dataSource1 = new List<int>() { 1, 2, 3, 4, 5, 6 };
            List<int> dataSource2 = new List<int>() { 1, 3, 5, 8, 9, 10 };

            //Method Syntax
            var MS = dataSource1.Intersect(dataSource2).ToList();

            //Query Syntax
            var QS = (from num in dataSource1
                      select num)
                      .Intersect(dataSource2).ToList();

            foreach (var item in MS)
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }
    }
}

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.

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).ToList();

            //Query Syntax
            var QS = (from country in dataSource1
                      select country)
                      .Intersect(dataSource2).ToList();

            foreach (var item in QS)
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }
    }
}

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.

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();

            foreach (var item in QS)
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }
    }
}

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.

namespace LINQDemo
{
    public class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
}

This is a very simple student class with just two properties. Let say, we have the following two data sources.

LINQ Intersect() Method with Complex Type

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 student’s names from both the collections.

using System.Collections.Generic;
using System;
using System.Linq;
namespace LINQDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Student> StudentCollection1 = new List<Student>()
            {
                new Student {ID = 101, Name = "Preety" },
                new Student {ID = 102, Name = "Sambit" },
                new Student {ID = 105, Name = "Hina"},
                new Student {ID = 106, Name = "Anurag"},             
            };

            List<Student> StudentCollection2 = new List<Student>()
            {
                new Student {ID = 105, Name = "Hina"},
                new Student {ID = 106, Name = "Anurag"},
                new Student {ID = 107, Name = "Pranaya"},
                new Student {ID = 108, Name = "Santosh"},
            };
            
            //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();

            foreach (var name in MS)
            {
                Console.WriteLine(name);
            }

            Console.ReadKey();
        }
    }
}

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.

using System.Collections.Generic;
using System;
using System.Linq;
namespace LINQDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Student> StudentCollection1 = new List<Student>()
            {
                new Student {ID = 101, Name = "Preety" },
                new Student {ID = 102, Name = "Sambit" },
                new Student {ID = 105, Name = "Hina"},
                new Student {ID = 106, Name = "Anurag"},             
            };

            List<Student> StudentCollection2 = new List<Student>()
            {
                new Student {ID = 105, Name = "Hina"},
                new Student {ID = 106, Name = "Anurag"},
                new Student {ID = 107, Name = "Pranaya"},
                new Student {ID = 108, Name = "Santosh"},
            };

            //Method Syntax
            var MS = StudentCollection1.Intersect(StudentCollection2).ToList();

            //Query Syntax
            var QS = (from std in StudentCollection1
                      select std).Intersect(StudentCollection2).ToList();

            foreach (var student in MS)
            {
                Console.WriteLine($" ID : {student.ID} Name : {student.Name}");
            }
            
            Console.ReadKey();
        }
    }
}

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.

using System.Collections.Generic;
using System;
using System.Linq;
namespace LINQDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Student> StudentCollection1 = new List<Student>()
            {
                new Student {ID = 101, Name = "Preety" },
                new Student {ID = 102, Name = "Sambit" },
                new Student {ID = 105, Name = "Hina"},
                new Student {ID = 106, Name = "Anurag"},             
            };

            List<Student> StudentCollection2 = new List<Student>()
            {
                new Student {ID = 105, Name = "Hina"},
                new Student {ID = 106, Name = "Anurag"},
                new Student {ID = 107, Name = "Pranaya"},
                new Student {ID = 108, Name = "Santosh"},
            };

            //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();
        }
    }
}

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.

using System.Collections.Generic;
namespace LINQDemo
{
    public class StudentComparer : IEqualityComparer<Student>
    {
        public bool Equals(Student x, Student y)
        {
            return x.ID == y.ID && x.Name == y.Name;
        }

        public int GetHashCode(Student obj)
        {
            return obj.ID.GetHashCode() ^ obj.Name.GetHashCode();
        }
    }
}

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.

using System.Collections.Generic;
using System;
using System.Linq;
namespace LINQDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Student> StudentCollection1 = new List<Student>()
            {
                new Student {ID = 101, Name = "Preety" },
                new Student {ID = 102, Name = "Sambit" },
                new Student {ID = 105, Name = "Hina"},
                new Student {ID = 106, Name = "Anurag"},             
            };

            List<Student> StudentCollection2 = new List<Student>()
            {
                new Student {ID = 105, Name = "Hina"},
                new Student {ID = 106, Name = "Anurag"},
                new Student {ID = 107, Name = "Pranaya"},
                new Student {ID = 108, Name = "Santosh"},
            };

            StudentComparer studentComparer = new StudentComparer();

            //Method Syntax
            var MS = StudentCollection1
                     .Intersect(StudentCollection2, studentComparer).ToList();

            //Query Syntax
            var QS = (from std in StudentCollection1
                      select std)
                      .Intersect(StudentCollection2, studentComparer).ToList();

            foreach (var student in QS)
            {
                Console.WriteLine($" ID : {student.ID} Name : {student.Name}");
            }

            Console.ReadKey();
        }
    }
}

In the next article, I am going to discuss the Union Method in LINQ with examples. Here, in this article, I try to explain the LINQ Intersect Method in C# with different types of examples.

Leave a Reply

Your email address will not be published. Required fields are marked *