Hi folks, In this tutorial, We are going to understand Distinct Operator in Linq which is comes under the Set operator’s category in LINQ query operators. A distinct operator is used to remove duplicate values from the collection and return the unique values of the collection.

What is Distinct Operator in Linq?

The distinct operator comes under the set operators category in Linq query operators which is used to remove duplicate values from the collection and return the unique values of the collection. The Distinct operator is only available in Method Syntax not in Query Syntax.

Example Of Distinct Operator in Linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

class Program
{
 
static void Main(string[] args)
{

IEnumerable<int> lstIntItems = new List<int> {10,23, 10,44,32,23,44 };

var vDistincIntItem = lstIntItems.Distinct();
Console.WriteLine("Distinct Integer Values");
foreach (int iValue in vDistincIntItem)
{
Console.WriteLine("{0}", iValue);
}
IEnumerable<string> lstStringItems = new List<string> { "One", "Two", "Three", "One", "Four", "Three", "Two" };
var vDistinctStringItem = lstStringItems.Distinct();
Console.WriteLine("Distinct String Values");
foreach (string sValue in vDistinctStringItem)
{
Console.WriteLine("{0}", sValue);
}
Console.ReadLine(); 
}
 
}

Distinct Operation on Complex Type:

By default, the distinct operation cannot perform on complex types for example if we have a collection of the Employee object then we cannot perform a distinct operation on this collection directly. For comparing the values of complex types we need to implement IEqualityComparer<T> interface where T can be any type. The IEqualityComparer<T> interface provides two methods:

  • Equals.

  • GetHashCode.

Example of IEqualityComparer:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;


public class Employee
{
public string EmployeeCode { get; set; }
public string EmployeeName { get; set; }
public string Gender { get; set; }
public string Designation { get; set; }
public double Salary { get; set; }
}
public class EmployeeComparer : IEqualityComparer<Employee>
{
public bool Equals(Employee x, Employee y)
{
if (string.Equals(x.EmployeeName, y.EmployeeName, StringComparison.OrdinalIgnoreCase))
return true;
return false; 
}

public int GetHashCode(Employee obj)
{
return obj.EmployeeName.GetHashCode();
}
}

class Program
{
public static List<Employee> DataSource()
{
List<Employee> lstEmployee = new List<Employee>();
Employee objEmployee1 = new Employee() { EmployeeCode = "E001", EmployeeName = "Ayush Singh", Gender = "Male", Designation = "Software Engineer", Salary = 30000 };
Employee objEmployee2 = new Employee() { EmployeeCode = "E002", EmployeeName = "Ritwik Nigam", Gender = "Male", Designation = "Software Engineer", Salary = 30000 };
Employee objEmployee3 = new Employee() { EmployeeCode = "E003", EmployeeName = "Nikhil Bakshi", Gender = "Male", Designation = "Team Lead", Salary = 60000 };
Employee objEmployee4 = new Employee() { EmployeeCode = "E001", EmployeeName = "Ayush Singh", Gender = "Male", Designation = "Software Engineer", Salary = 30000 };
Employee objEmployee5 = new Employee() { EmployeeCode = "E002", EmployeeName = "Ritwik Nigam", Gender = "Male", Designation = "Software Engineer", Salary = 30000 };
Employee objEmployee6 = new Employee() { EmployeeCode = "E004", EmployeeName = "Neha Sharma", Gender = "Female", Designation = "Graphic Designer", Salary = 25000 };
Employee objEmployee7 = new Employee() { EmployeeCode = "E003", EmployeeName = "Nikhil Bakshi", Gender = "Male", Designation = "Team Lead", Salary = 60000 };
lstEmployee.Add(objEmployee1);
lstEmployee.Add(objEmployee2);
lstEmployee.Add(objEmployee3);
lstEmployee.Add(objEmployee4);
lstEmployee.Add(objEmployee5);
lstEmployee.Add(objEmployee6);
lstEmployee.Add(objEmployee7);
return lstEmployee;
}
static void Main(string[] args)
{

var distinctEmployees = DataSource().Distinct(new EmployeeComparer());
foreach (Employee objEmployee in distinctEmployees)
{
Console.WriteLine("Employee Code {0}, Employee Name {1}, Employee Salary", objEmployee.EmployeeCode, objEmployee.EmployeeName, objEmployee.Salary);
}
Console.ReadLine(); 
}
 
}

Output:

Employee Code E001, Employee Name Ayush Singh, Employee Salary
Employee Code E002, Employee Name Ritwik Nigam, Employee Salary
Employee Code E003, Employee Name Nikhil Bakshi, Employee Salary
Employee Code E004, Employee Name Neha Sharma, Employee Salary

View More:

Conclusion:

I hope you would love this post. Please don’t hesitate to comment for any technical help. Your feedback and suggestions are welcome to me.

Thank You.

Leave a Comment