Skip to content
Related Articles

Related Articles

Improve Article

LINQ | Set Operator | Distinct

  • Last Updated : 22 May, 2019

In LINQ, Set operators are those operators in query expression which return a result set which is based on the existence or non-existence of the equivalent elements within the same or different collections or sequences or sets. The standard query operator contains the following set operators:

  1. Union
  2. Intersect
  3. Except
  4. Distinct

Distinct Operator

The Distinct operator returns the set which does not contain duplicate values. Or in other words, we can say that this operator removes all the duplicate values from the set or collection and return a set or collection which contain unique or dissimilar values.

  • It does not support query syntax in C# and VB.Net languages. But you can use the Distinct method on query variable or you can wrap your query in brackets and then the call Distinct method.
  • It support method syntax in both C# and VB.Net languages.
  • It present in both the Queryable and Enumerable class.
  • It is implemented by using deferred execution.
  • When you are working with the collections of complex types, then you must use IEqualityComparer interface to use Distinct method because Distinct method does not compare the values of complex types.

Example 1:




// C# program to find the sequence 
// which contains unique elements
using System;
using System.Linq;
  
class GFG {
  
    static public void Main()
    {
  
        // Data source
        char[] sequence = {'m', 'q', 'o', 's', 'y'
                          'a', 'a', 'c', 'y', 'o'};
  
        // Display the sequence
        Console.WriteLine("Sequence is: ");
  
        foreach(var s in sequence)
        {
            Console.WriteLine(s);
        }
  
        // Get the sequence which contains unique 
        // elements Using Distinct function
        var result = sequence.Distinct();
  
        Console.WriteLine("New Sequence: ");
  
        foreach(var val in result)
        {
            Console.WriteLine(val);
        }
    }
}
Output:
Sequence is: 
m
q
o
s
y
a
a
c
y
o
New Sequence: 
m
q
o
s
y
a
c

Example 2:




// C# program to find the salary of 
// the employees with dissimilar values
using System;
using System.Linq;
using System.Collections.Generic;
  
// Employee details
public class Employee {
  
    public int emp_id
    {
        get;
        set;
    }
  
    public string emp_name
    {
        get;
        set;
    }
  
    public string emp_gender
    {
        get;
        set;
    }
  
    public string emp_hire_date
    {
        get;
        set;
    }
  
    public int emp_salary
    {
        get;
        set;
    }
}
  
class GFG {
  
    // Main method
    static public void Main()
    {
        List<Employee> emp = new List<Employee>() {
              
            new Employee() {emp_id = 209, emp_name = "Anjita", emp_gender = "Female",
                                    emp_hire_date = "12/3/2017", emp_salary = 20000},
                                      
            new Employee() {emp_id = 210, emp_name = "Soniya", emp_gender = "Female",
                                    emp_hire_date = "22/4/2018", emp_salary = 30000},
                                      
            new Employee() {emp_id = 211, emp_name = "Rohit", emp_gender = "Male",
                                  emp_hire_date = "3/5/2016", emp_salary = 40000},
                                    
            new Employee() {emp_id = 212, emp_name = "Supriya", emp_gender = "Female",
                                      emp_hire_date = "4/8/2017", emp_salary = 40000},
                                        
            new Employee() {emp_id = 213, emp_name = "Anil", emp_gender = "Male",
                                emp_hire_date = "12/1/2016", emp_salary = 40000},
                                  
            new Employee() {emp_id = 214, emp_name = "Anju", emp_gender = "Female"
                                  emp_hire_date = "17/6/2015", emp_salary = 50000},
        };
  
        // Query to find the salary of the
        // employees with dissimilar values
        // Using Distinct method
        var res = emp.Select(e => e.emp_salary).Distinct();
          
        Console.WriteLine("Employee Salary: ");
          
        foreach(var val in res)
        {
            Console.WriteLine(val);
        }
    }
}
Output:
Employee Salary: 
20000
30000
40000
50000



My Personal Notes arrow_drop_up
Recommended Articles
Page :