Paul
Paul

Reputation: 1235

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

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

Upvotes: 3

Views: 9345

Answers (1)

Amy B
Amy B

Reputation: 110161

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;
  }
}

Upvotes: 6

Related Questions