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

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

Here we will find all unique integer numbers using Linq Distinct() method. Then we will print all the distinct values on the console screen.

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 + " ");
} 




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.