In this tutorial, let me show you how to delete duplicates elements from list using IEqualityComparer in ASP.NET 4.6. IEqualityComparer is implemented by classes that need to support an equality comparison for two values of the same type. Generic collections require instances of classes that implement the IEqualityComparer interface in order to provide support for custom data types. IEqualityComparer has two methods to support the comparison of objects for equality. Let us suppose that you have a Employee class that has four properties EmployeeID,FirstName,LastName and Age. Now, you need to write the following code:

public class Employee
{
public Employee()
{
}
public string EmployeeID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public List<Employee> Employees
{
    get
    {
return new List<Employee>()
        {
new Employee(){EmployeeID="01",FirstName="Peter",LastName="Last01",Age=31},
new Employee(){EmployeeID="02",FirstName="Scott",LastName="Last02",Age=35},
new Employee(){EmployeeID="03",FirstName="Rebecca",LastName="Last03",Age=36},
new Employee(){EmployeeID="01",FirstName="Paul",LastName="Last01",Age=31},
new Employee(){EmployeeID="01",FirstName="Richard",LastName="Last01",Age=31},
        };
    }
}
}

In the above class I have added some dummy records which have duplicates elements. Now,let's remove the duplicate elements from the above class using IEqualityComparer interface.  For this right click on the project and add a new class named EmployeeEquality and inherit this class with IEqualityComparer and write the below code:
public class EmployeeEquality : IEqualityComparer<Employee>
{
public bool Equals(Employee x, Employee y)
{
if (x.EmployeeID == y.EmployeeID && x.FirstName == y.FirstName && x.LastName == y.LastName && x.Age == y.Age)
return true;
return false;
}
public int GetHashCode(Employee obj)
{
//For shake of simplicity
return obj.FirstName.GetHashCode();
}
}


Now,let's display the record on UI. First, right click on the project and add new page,and add the below code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:Button ID="btnUnSorted" runat="server" OnClick="btnUnSorted_Click" Text="Duplicates" />
<asp:Button ID="btnWDuplicate" runat="server" OnClick="btnWDuplicate_Click" Text="Without Duplicate" />
</form>
</body>
</html>

In the code behind add following code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUnSorted_Click(object sender, EventArgs e)
{
    Employee employee = new Employee();
    GridView1.DataSource = employee.Employees;
    GridView1.DataBind();
}
protected void btnWDuplicate_Click(object sender, EventArgs e)
{
    Employee employee = new Employee();
    List<Employee> list = employee.Employees;
    var distinctEmployee = list.Distinct(new EmployeeEquality());
    GridView1.DataSource = distinctEmployee;
    GridView1.DataBind();
}
}

HostForLIFE.eu ASP.NET 4.6 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.