Using LinQ I'd like to query the list and find the duplicate persons (a duplicate is defined as having same first, last name and date of birth) and mark each of the duplicate person's StateOfData property with the string "duplicate" and each unique person's StateOfData property with the string "unique".
public Class Person
{
public string PersonFirstName { get; set; }
public string PersonLastName { get; set; }
public datetime PersonDateOfBirth { get; set; }
public string StateOfData{ get; set }
public Person (string firstName, string lastName, dateTime dateOfBirth,string state)
{
this.PersonFirstName = firstName;
this.PersonLastName = lastName;
this.PersonDateOfBirth = dateOfBirth;
this.StateOfData = state;
}
}
IList<Person> personsList = new List<Person>();
Person pers = new Person("Diane","Jones","1967-01-01","");
personsList.add(pers);
Person pers = new Person("Diane","Jones","1967-01-01","");
personsList.add(pers);
Person pers = new Person("John","Jones","1967-01-01","");
personsList.add(pers);
The result of persons list should read:
"Diane","Jones","1967-01-01","duplicate"
"Diane","Jones","1967-01-01","duplicate"
"John","Jones","1967-01-01","unique"
Any help with this would be greatly appreciated
var theLookup = personList
.GroupBy(p => new {p.PersonFirstName, p.PersonLastName, p.PersonDateOfBirth})
.ToLookup(g => g.Skip(1).Any() ? "duplicate" : "unique");
foreach(var lookupEntry in theLookup)
{
string stateOfData = lookupEntry.Key;
foreach(Person p in lookupEntry.SelectMany(g => g))
{
p.StateOfData = stateOfData;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With