C# program to demonstrate the example of Linq Intersect() method

Here, we are going to learn about the Linq Intersect() method and its C# implementation with an example.
Submitted by Nidhi, on August 28, 2020

Here we will find common integer numbers of two lists using the Linq Intersect() method. Then we will print common numbers on the console screen.

Program:

The source code to demonstrate Linq Intersect() method, is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# Program to demonstrate Linq Intersect() method.

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

class Demo
{
    static void Main(string[] args)
    {
        List<int> List1 = new List<int>() { 10, 20, 30, 40, 50 };
        List<int> List2 = new List<int>() { 10, 40, 60, 80, 90 };

        var result = List1.Intersect(List2);

        foreach (var value in result)
        {
            Console.WriteLine(value + " ");
        } 
    }
}

Output:

10
40
Press any key to continue . . .

Explanation:

In the above program, we created two lists of integer numbers that are given below.

List<int> List1 = new List<int>() { 10, 20, 30, 40, 50 };
List<int> List2 = new List<int>() { 10, 40, 60, 80, 90 };

Here we used Linq Intersect() method to find common values of two lists using the below code.

var result = List1.Intersect(List2);

Then we print common values on the console screen using the below code.

foreach (var value in result)
{
    Console.WriteLine(value + " ");
} 

C# LINQ Programs »


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.