Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Linq to group by multiple columns in a list and mark all duplicate items

Tags:

c#

class

linq

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

like image 717
Paul Avatar asked Dec 10 '25 03:12

Paul


1 Answers

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;
  }
}
like image 54
Amy B Avatar answered Dec 12 '25 17:12

Amy B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!