c sharp

C# LINQ Intersect() Method

Language Integrated Query language (LINQ) is used to perform operations on the C# collections or Normal data structures. It is used to perform queries similar to SQL Like expressions.

In this article, we will discuss how to perform Intersection operation using Intersect() method through LINQ.

LINQ Intersect()

Intersect() in LINQ is used to return the common elements from two given data structures. The elements can be numeric type or string type.

Syntax

Data1.Intersect(Data2)

Data1 is the first data structure and Data2 is the second data structure.

Example 1

Let’s create two list data structures with Integer elements and return the common values using Intersect() method.

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class - Linuxhint
class Linuxhint
{
 
    static public  void Main(){
       
        //create first data
       List first_list = new List() { 345,678,456,890 };
       
       //create second  data
       List second_list = new List() { 345,890,324,564,433};
       
       Console.WriteLine("First List: ");
           foreach (var values in first_list)
            {
                Console.WriteLine(values);
            }
            Console.WriteLine("Second List: ");
           foreach (var values in second_list)
            {
                Console.WriteLine(values);
            }
           
        //perform Intersect operation on two Lists and store the result into list.
       var operation = first_list.Intersect(second_list).ToList();
           
         Console.WriteLine("Intersection operation on two Integer Lists: ");
         
         //return one by one value from the operation
        foreach (var result in operation)
            {
                Console.WriteLine(result);
            }
           
}
}

Output

Explanation

Lists Creation
We have to specify the data type inside List.

Intersect Operation
After performing Intersect operation, we will store the result into List.

Result
We are displaying one by one elements using the foreach() loop.

The common elements on both the Lists are: 345 and 890.

Example 2

Let’s create two list data structures with double elements and return the common values using Intersect() method.

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class - Linuxhint
class Linuxhint
{
 
    static public  void Main(){
       
        //create first data
       List first_list = new List() { 1.20,4.5,7.0,8.99 };
       
       //create second data
       List second_list = new List() { 1.20,4.5,7.0,8.99};
       
       Console.WriteLine("First List: ");
           foreach (var values in first_list)
            {
                Console.WriteLine(values);
            }
            Console.WriteLine("Second List: ");
           foreach (var values in second_list)
            {
                Console.WriteLine(values);
            }
           
        //perform Intersect operation on two Lists and store the result into list.
       var operation = first_list.Intersect(second_list).ToList();
           
         Console.WriteLine("Intersection operation on two double type Lists: ");
         
         //return one by one value from the operation
        foreach (var result in operation)
            {
                Console.WriteLine(result);
            }
           
}
}

Output

Explanation

Lists Creation
We have to specify the data type double inside the List and pass the elements.

Intersect Operation
After performing Intersect operation, we will store the result into List.

Result
We are displaying one by one element using the foreach() loop.

The common elements on both the Lists are: 1,2,4,5,7 and 8.99. 

Example 3

Let’s create two list data structures with string elements and return the common values using Intersect() method.

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class - Linuxhint
class Linuxhint
{
 
    static public  void Main(){
       
        //create first data
       List first_list = new List() {"welcome","to","Linuxhint" };
       
       //create second data
       List second_list = new List() { "Linuxhint"};
       
       Console.WriteLine("First List: ");
           foreach (var values in first_list)
            {
                Console.WriteLine(values);
            }
            Console.WriteLine("Second List: ");
           foreach (var values in second_list)
            {
                Console.WriteLine(values);
            }
           
        //perform Intersect operation on two Lists and store the result into list.
       var operation = first_list.Intersect(second_list).ToList();
           
         Console.WriteLine("Intersection operation on two double type Lists: ");
         
         //return one by one value from the operation
        foreach (var result in operation)
            {
                Console.WriteLine(result);
            }
           
}
}

Output

The common elements on both the Lists are: Linuxhint.

Conclusion

We have seen how to perform Intersect() operation on two data structures in C# using LINQ. Here, we consider List as a data structure.Intersect() in LINQ is used to return the common elements from two given data structures. The elements can be numeric type or string type.

We discussed three examples with different data type elements.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain