Using c# 3 and .Net Framework 3.5, I have a Person object
public Person { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public int SSN { get; set; } }
and I’ve got a List of them:
List<Person> persons = GetPersons();
How can I get all the Person objects in persons where SSN is not unique in the list and remove them from the persons list and ideally add them to another list called ‘List<Person> dupes‘?
The original list might look something like this:
persons = new List<Person>(); persons.Add(new Person { Id = 1, FirstName = 'Chris', LastName='Columbus', SSN=111223333 }); // Is a dupe persons.Add(new Person { Id = 1, FirstName = 'E.E.', LastName='Cummings', SSN=987654321 }); persons.Add(new Person { Id = 1, FirstName = 'John', LastName='Steinbeck', SSN=111223333 }); // Is a dupe persons.Add(new Person { Id = 1, FirstName = 'Yogi', LastName='Berra', SSN=123456789 });
And the end result would have Cummings and Berra in the original persons list and would have Columbus and Steinbeck in a list called dupes.
Many thanks!
This gets you the duplicated SSN:
The duplicated list would be like:
And then just iterate over the duplicates and remove them.