In this post, I will show you how to remove the duplicate record from the List using IEqualityComparer.It has two methods to support the comparison of objects for equality.
Let us suppose that you have an Employee class that has four properties. EmployeeID,FirstName,LastName and Age
.
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="E001",FirstName="F001",LastName="L001",Age=21},
new Employee(){EmployeeID="E002",FirstName="F002",LastName="L002",Age=25},
new Employee(){EmployeeID="E003",FirstName="F003",LastName="L003",Age=26},
new Employee(){EmployeeID="E001",FirstName="F001",LastName="L001",Age=21},
new Employee(){EmployeeID="E001",FirstName="F001",LastName="L001",Age=21},
};
}
}
}
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 implement the interface method as shown below.
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
Right click on the project and add new page,and add following code inside it
<%@ 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();
}
}