Q:

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

belongs to collection: C# LINQ Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

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

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

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

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

        var result = List1.Intersect(List2).OrderBy(lst => lst);

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

Output:

10
20
30
40
Press any key to continue . . .

Explanation:

In the above program, we created a class Demo that contains the Main() method. In the Main() method we created two lists of integers.

var result = List1.Intersect(List2).OrderBy(lst => lst);

In the above code, we find common items from both lists using the Intersect() method and then sort them using OrderBy() method.

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

In the above code, we accessed the filtered values one by one and print it on the console screen.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# LINQ Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to demonstrate the use of Linq OrderByD... >>
<< C# program to find integer numbers from the list o...