How to Remove Objects with a Duplicate Property from a List in C#

Have you ever encountered a situation where you have a list of objects and you want to remove the objects that have duplicate values for a specific property? In this article, I will show you how to achieve this in C# without using any third-party libraries.

What is the Problem?

Let’s say you have a list of Person objects, and each Person has a unique Id property. However, there might be some objects in the list that have the same Id value. Your goal is to remove these duplicate objects and keep only one instance of each Id value in the list.

How to Solve the Problem?

To solve this problem, we can make use of the LINQ (Language Integrated Query) extension methods provided by C#. Here’s the step-by-step process to remove objects with a duplicate property from a list:

  1. Group the objects in the list by the property you want to check for duplicates. In our case, it’s the Id property of the Person objects.
  2. Select the first object from each group, as it will be the one with the unique value for the property.
  3. Convert the result into a new list.

Here’s the code that accomplishes this:

var uniquePersons = persons.GroupBy(p => p.Id)
                          .Select(g => g.First())
                          .ToList();

Let’s break down the code and understand each step:

  • persons is the original list of Person objects.
  • GroupBy(p => p.Id) groups the objects in the list based on their Id property.
  • Select(g => g.First()) selects the first object from each group, effectively removing the duplicates.
  • ToList() converts the result into a new list.

Example

Let’s consider an example to see how this code works. Suppose we have the following list of Person objects:

List<Person> persons = new List<Person>
{
    new Person { Id = 1, Name = "John" },
    new Person { Id = 2, Name = "Jane" },
    new Person { Id = 1, Name = "Alice" },
    new Person { Id = 3, Name = "Bob" },
    new Person { Id = 2, Name = "Eve" }
};

After applying the code mentioned earlier, the uniquePersons list will contain the following objects:

[
    { Id = 1, Name = "John" },
    { Id = 2, Name = "Jane" },
    { Id = 3, Name = "Bob" }
]

As you can see, the duplicate objects with Id values of 1 and 2 have been removed, and only one instance of each unique Id value remains in the uniquePersons list.

Conclusion

In this article, we learned how to remove objects with a duplicate property from a list in C#. By utilizing the power of LINQ, we were able to achieve this without relying on any third-party libraries. The code provided is simple and efficient, making it a convenient solution for handling such scenarios in your C# projects.

Categories C#

Related Posts

How to Set Column Header Text for a Specific Column in DataGridView in C#

Prerequisites To follow along with this tutorial, you will need a basic understanding of C# programming and the Windows Forms framework. Step-by-Step Guide Step 1: Create a new Windows Forms application First, let’s create a new Windows Forms application in Visual Studio. Open Visual Studio and select “Create a new project”. Choose the “Windows Forms ...

Read more

How to Create an Excel (.XLS and .XLSX) File in C# without Installing Microsoft Office

If you’re a C# developer looking to create Excel files without the need to install Microsoft Office, you’re in luck! There are several libraries available that can help you achieve this task easily and efficiently. In this article, we will explore two popular libraries, ExcelLibrary and EPPlus, and discuss how to use them to create ...

Read more

Why would one use Task over ValueTask in C#?

When working with asynchronous programming in C#, developers have the option to use either Task<T> or ValueTask<T> to represent the result of an asynchronous operation. Both types provide similar functionality, but there are certain scenarios where using Task<T> is preferred over ValueTask<T>. In this article, we will explore the reasons why one would choose Task<T> ...

Read more

Better Way to Cast Object to Int in C#

Have you ever encountered a situation where you needed to cast an object to an integer in your C# code? If so, you may have wondered what the best approach is to achieve this. In this article, we will explore different methods to cast an object to an int in C# and discuss the scenarios ...

Read more

Leave a Comment