How to get a distinct list from a List of objects in C#?

How to get a distinct list from a List of objects in C#?

To get a distinct list from a List of objects in C# based on specific properties of the objects, you can use LINQ's Distinct method with a custom IEqualityComparer. Here's an example:

Suppose you have a class Person with properties Id and Name:

public class Person { public int Id { get; set; } public string Name { get; set; } } 

And you have a List<Person>:

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

Now, if you want to get a distinct list of Person objects based on their Id, you can use LINQ's Distinct method with a custom IEqualityComparer<Person>:

using System; using System.Collections.Generic; using System.Linq; public class Program { public static void Main() { List<Person> people = new List<Person> { new Person { Id = 1, Name = "John" }, new Person { Id = 2, Name = "Jane" }, new Person { Id = 1, Name = "John" }, new Person { Id = 3, Name = "Alice" } }; // Get a distinct list of Person objects based on their Id List<Person> distinctPeople = people.Distinct(new PersonEqualityComparer()).ToList(); // Print the distinct list foreach (var person in distinctPeople) { Console.WriteLine($"Id: {person.Id}, Name: {person.Name}"); } } } public class PersonEqualityComparer : IEqualityComparer<Person> { public bool Equals(Person x, Person y) { return x.Id == y.Id; } public int GetHashCode(Person obj) { return obj.Id.GetHashCode(); } } 

In this example, we create a custom IEqualityComparer<Person> named PersonEqualityComparer that compares Person objects based on their Id property. We then use this custom comparer in the Distinct method to get a distinct list of Person objects based on their Id.

The output of the code will be:

Id: 1, Name: John Id: 2, Name: Jane Id: 3, Name: Alice 

Examples

  1. "C# distinct list from List of objects"

    Description: This query focuses on obtaining a distinct list of objects based on a specific property in C#. The following code snippet demonstrates how to achieve this using LINQ's Distinct method along with a custom equality comparer.

    // C# code to get distinct list from List of objects using System; using System.Collections.Generic; using System.Linq; public class DistinctListHelper { public static List<T> GetDistinctList<T, TKey>(List<T> list, Func<T, TKey> keySelector) { return list.GroupBy(keySelector).Select(group => group.First()).ToList(); } } 

    This code defines a DistinctListHelper class with a method GetDistinctList that takes a List<T> and a key selector function as input. It then groups the list by the selected key and returns the first element of each group, resulting in a distinct list based on the specified property.

  2. "C# LINQ distinct list from List of objects"

    Description: This query seeks information on using LINQ to obtain a distinct list of objects in C#. The code snippet below demonstrates how to achieve this using the Distinct method.

    // C# code for LINQ distinct list from List of objects using System; using System.Collections.Generic; using System.Linq; public class LinqDistinctList { public static List<T> GetDistinctList<T>(List<T> list) { return list.Distinct().ToList(); } } 

    This code presents a LinqDistinctList class with a method GetDistinctList that takes a List<T> as input and returns a distinct list of objects using the LINQ Distinct method.

  3. "C# get distinct values from List of objects by property"

    Description: This query focuses on obtaining distinct values from a list of objects based on a specific property in C#. Below is a code snippet demonstrating how to achieve this using LINQ.

    // C# code to get distinct values from List of objects by property using System; using System.Collections.Generic; using System.Linq; public class DistinctValuesByProperty { public static List<T> GetDistinctValues<T, TKey>(List<T> list, Func<T, TKey> keySelector) { return list.GroupBy(keySelector).Select(group => group.First()).ToList(); } } 

    This code defines a DistinctValuesByProperty class with a method GetDistinctValues that takes a List<T> and a key selector function as input. It then groups the list by the selected key and returns the first element of each group, resulting in a distinct list based on the specified property.

  4. "C# distinct list from List of custom objects"

    Description: This query is interested in obtaining a distinct list of custom objects in C#. Below is a code snippet demonstrating how to achieve this using the Distinct method.

    // C# code for distinct list from List of custom objects using System; using System.Collections.Generic; using System.Linq; public class CustomObjectDistinctList { public static List<CustomObject> GetDistinctList(List<CustomObject> list) { return list.Distinct().ToList(); } } 

    This code presents a CustomObjectDistinctList class with a method GetDistinctList that takes a List<CustomObject> as input and returns a distinct list of custom objects using the Distinct method.


More Tags

facebook-prophet turkish core-location prepared-statement centos6.5 tibble rest-client atom-feed inner-classes rpa

More C# Questions

More Chemistry Calculators

More Fitness-Health Calculators

More Retirement Calculators

More Biochemistry Calculators