Q:

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

0

C# program to demonstrate the example of Linq Distinct() 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 Distinct() method, is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to demonstrate Linq Distinct() method.

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

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


        var result = Numbers.Distinct();

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

Output:

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

Explanation:

In the above program, we created the list of integer numbers.

List<int> Numbers = new List<int>() { 10, 20, 20, 30, 40, 40, 10, 50, 60 };

In the above list, some values are duplicates. Then we find all unique values using the below code.

var result = Numbers.Distinct();

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

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

 

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

total answers (1)

Similar questions


need a help?


find thousands of online teachers now