How to use Distinct() with lambda expression to filter duplicates by object property?
Hello everyone! I'm new to C# and encountered a problem when working with collections. I have a list of Person class objects:
public class Person\n{\n public int Id { get; set; }\n public string Name { get; set; }\n public int Age { get; set; }\n}\n\nI populate the list with data, and it ends up having duplicates by the Name field. I need to get only unique names, but keep the objects intact (or at least select one of the duplicates).
I know there is a Distinct() method, but it only works for primitive types or if you override Equals and GetHashCode in the class. And I don't want to modify the Person class — it is used in other places, and comparison by all fields
No answers yet.
Log in to leave an answer