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

C# Triple Double Quotes: What are they and how to use them?

In C# programming language, triple double quotes (“””) are a special syntax known as raw string literals. They provide a convenient way to work with strings that contain quotes or embedded language strings like JSON, XML, HTML, SQL, Regex, and others. Raw string literals eliminate the need for escaping characters, making it easier to write ...

Read more

Best Practices in Using a Lock in C#

What is a Lock? A lock in C# is implemented using the lock keyword, which ensures that only one thread can enter a specific section of code at a time. When a thread encounters a lock statement, it attempts to acquire a lock on the specified object. If the lock is already held by another ...

Read more

Usage of ‘&’ versus ‘&&’ in C#

‘&’ Operator The ‘&’ operator in C# is a bitwise AND operator. It operates at the bit level, meaning that it performs the AND operation on each corresponding pair of bits in the operands. This operator is commonly used when working with binary data or performing low-level bit manipulation. For example, consider the following code ...

Read more

How to Add a Badge to a C# WinForms Control

Have you ever wanted to add a badge to a C# WinForms control? Maybe you want to display a notification count on a button or indicate the status of a control. In this article, I will show you how to easily add a badge to a C# WinForms control using a static Adorner class. What ...

Read more

Leave a Comment