C
C#3y 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
TheRanger3y ago
look up LINQ's Distinct method
Pobiega
Pobiega3y 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
TheRanger3y ago
or if the Equals Method is overridden
Accord
Accord3y ago
✅ This post has been marked as answered!

Did you find this page helpful?