So, for this article first we will create a console application with c#.net. After creating the console application, we will add a class file named as EmployeeModel and add the below code in it.
public class EmployeeModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Email { get; set; }
}
In above I have declared the propertied in the class. Now let's add some duplicate value. List<EmployeeModel> employees = new List<EmployeeModel>() {
new EmployeeModel { Id=1,Name="John" ,Address="Address1",Email="john@mail.com"},
new EmployeeModel { Id=2,Name="Mate" ,Address="Address2",Email="mate@mail.com"},
new EmployeeModel { Id=2,Name="Mate" ,Address="Address2",Email="mate@mail.com"},
new EmployeeModel { Id=3,Name="Rose" ,Address="Address3",Email="rose@mail.com"},
new EmployeeModel { Id=1,Name="John" ,Address="Address1",Email="john@mail.com"},
new EmployeeModel { Id=4,Name="Pape" ,Address="Address4",Email="pape@mail.com"},
new EmployeeModel { Id=5,Name="Pranav" ,Address="Address5",Email="pranav@mail.com"}
};
Method 1: DistinctBy for List Object
In above list record for employee if 1 and 2 is duplicate. Please check the below complete code where I have removed all the duplicate record from the list. List<EmployeeModel> employees = new List<EmployeeModel>() {
new EmployeeModel { Id=1,Name="John" ,Address="Address1",Email="john@mail.com"},
new EmployeeModel { Id=2,Name="Mate" ,Address="Address2",Email="mate@mail.com"},
new EmployeeModel { Id=2,Name="Mate" ,Address="Address2",Email="mate@mail.com"},
new EmployeeModel { Id=3,Name="Rose" ,Address="Address3",Email="rose@mail.com"},
new EmployeeModel { Id=1,Name="Mate" ,Address="Address1",Email="mate@mail.com"},
new EmployeeModel { Id=4,Name="Pape" ,Address="Address4",Email="pape@mail.com"},
new EmployeeModel { Id=5,Name="Pranav" ,Address="Address5",Email="pranav@mail.com"}
};
var data = employees.DistinctBy(m=>m.Id);
foreach (var item in data)
{
Console.WriteLine(string.Format("Id: {0}, Name: {1}", item.Id, item.Name));
}
Console.ReadLine();
In above code I have taken the list object. To remove the duplicate item from the list object DistinctBy(expression) method have been used. If you are using DistinctBy(expression) methos on that case the complete row record must be same as compared to others. Now let's run the code to check the output. Here whatever expression duplicate will be removed by that. Does not matter what the other property having the value. In above code we are getting all the distinct record from the list, here we have distinct record on the bases of Employee Id. Now press F5 to check the output.
Method 2: Distinct for String List
In this methos we will show we can get the distinct list data from the list of a string type using c#.net with the help of Distinct() method. Now let's take the check the list.
List<string> employees = new List<string>()
{
"John",
"Mate",
"Mate",
"Rose",
"John",
"Pape",
"Pranav",
};
Here you can see the list having the string value. Please check the highlighted values which are duplicate. Now let's check the code to get the distinct string list. List<string> employees = new List<string>()
{
"John",
"Mate",
"Mate",
"Rose",
"John",
"Pape",
"Pranav",
};
var data = employees.Distinct();
foreach (var item in data)
{
Console.WriteLine(string.Format("Name: {0}", item));
}
Console.ReadLine();
In above code I have used Distinct() to remove the duplicate or repeater record or data from the string list. Now let's check the output.