LINQ Distinct() Method
Language Integrated Query language (LINQ) is used to perform operations on the C# collections or Normal data structures.
LINQ Distinct()
Distinct() in LINQ is used to return the unique values present in a data structure (List). If there are any similar values, then those will be removed.
It is possible to use this method in two ways. Let’s look into it.
Approach 1: Using Method
We will use the normal Distinct() method to return distinct values.
Syntax:
Where list is the List object created.
Approach 2: Using Query
We will use the Query similar to SQL expression that returns the unique values.
Syntax:
from i in list select i.Distinct()
Where list is the List object created and iterator iterates the elements in a list..
Example 1:
Here, we will create a list that holds integer elements and we will use Distinct() to return only the unique values.
using System.Linq;
using System.Collections.Generic;
//create a class - Linuxhint
class Linuxhint
{
static public void Main(){
//create data
List first_list = new List() {23,23,45,67,89,10,10,22,23,45};
Console.WriteLine("List: ");
foreach (var values in first_list)
{
Console.WriteLine(values);
}
//get the unique elements from the list
var result = first_list.Distinct();
Console.WriteLine("Unique values from the list: ");
foreach (var values in result)
{
Console.WriteLine(values);
}
}
}
Output:
Explanation:
So first, we created a list data structure with 10 integers that include duplicates.
After that, we applied Distinct() to get the unique elements.
Finally, we can display the result using a foreach loop.
Example 2:
Here, we will create a list that holds string elements and we will use Distinct() to return only the unique values.
using System.Linq;
using System.Collections.Generic;
//create a class - Linuxhint
class Linuxhint
{
static public void Main(){
//create data
List first_list = new List() {"linuxhint","linuxhint","sravan","vignan","java","java"};
Console.WriteLine("List: ");
foreach (var values in first_list)
{
Console.WriteLine(values);
}
//get the unique elements from the list
var result = first_list.Distinct();
Console.WriteLine("Unique strings from the list: ");
foreach (var values in result)
{
Console.WriteLine(values);
}
}
}
Output:
Explanation:
So first, we created a list data structure with 6 strings that include duplicates.
After that, we applied Distinct() to get the unique elements.
Finally, we can display the result using a foreach loop.
Example 3:
Here, we will create a list that holds integer elements and we will use Distinct() with Query to return only the unique values.
using System.Linq;
using System.Collections.Generic;
//create a class - Linuxhint
class Linuxhint
{
static public void Main(){
//create data
List first_list = new List() {100,200,300,200,300};
Console.WriteLine("List: ");
foreach (var values in first_list)
{
Console.WriteLine(values);
}
//get the unique elements from the list
var result = (from i in first_list select i).Distinct();
Console.WriteLine("Unique values from the list: ");
foreach (var values in result)
{
Console.WriteLine(values);
}
}
}
Output:
Explanation:
So first, we created a list data structure with 10 integers that include duplicates.
After that, we applied Distinct() to get the unique elements.
Finally, we can display the result using a foreach loop.
Conclusion
Distinct() in LINQ is used to return the unique values present in a data structure. There are two ways to use the LINQ Distinct() method. We discussed three different examples with different data types. Make sure that use has to include – using System.Linq and using System.Collections.Generic command lines in your code.
Source: linuxhint.com