C
C#2y ago
zxa4fd

How to filter list so that if there are objects with same values in fields? [Answered]

Is there a way to filter objects so that objects with same field values are not duplicated in a list? ```cs var a = new Person(){Name="john", Age=22}; var b = new Person(){Name="john", Age=22}; var myList = new List<Person>(){a,b}; // what LINQ code to put here so that there are no duplicates?
4 Replies
TheRanger
TheRanger2y ago
look up LINQ's Distinct method
Pobiega
Pobiega2y ago
What if the name is the same but the age is different? or vice versa are those considered unique values? Distinct() works fine if Person is a record, btw as it supports value equality out of the box
TheRanger
TheRanger2y ago
or if the Equals Method is overridden
Accord
Accord2y ago
✅ This post has been marked as answered!