La réponse dans l'autre article est très bien pour votre problème aussi, que ce que vous voulez vraiment est une jointure. Il est important de noter que la jointure interne n'est utilisée que pour exécuter la fonction, elle ne modifie pas la liste (c'est-à-dire que les éléments qui ne rencontrent pas la jointure interne sont laissés intacts dans la liste).
Pour être complet, voici la solution que j'utiliserais:
List<Person> people = new List<Person>();
people.Add(new Person{ Name = "Timothy", Rating = 2 });
people.Add(new Person{ Name = "Joe", Rating = 3 });
people.Add(new Person{ Name = "Dave", Rating = 4 });
List<Person> updatedPeople = new List<Person>();
updatedPeople.Add(new Person { Name = "Timothy", Rating = 1 });
updatedPeople.Add(new Person { Name = "Dave", Rating = 2 });
ShowPeople("Full list (before changes)", people);
Func<Person, Person, Person> updateRating =
(personToUpdate, personWithChanges) =>
{
personToUpdate.Rating = personWithChanges.Rating;
return personToUpdate;
};
var updates = from p in people
join up in updatedPeople
on p.Name equals up.Name
select updateRating(p, up);
var appliedChanges = updates.ToList();
ShowPeople("Full list (after changes)", people);
ShowPeople("People that were edited", updatedPeople);
ShowPeople("Changes applied", appliedChanges);
Voici le résultat que je reçois:
Full list (before changes)
-----
Name: Timothy, Rating: 2
Name: Joe, Rating: 3
Name: Dave, Rating: 4
Full list (after changes)
-----
Name: Timothy, Rating: 1
Name: Joe, Rating: 3
Name: Dave, Rating: 2
People that were edited
-----
Name: Timothy, Rating: 1
Name: Dave, Rating: 2
Changes applied
-----
Name: Timothy, Rating: 1
Name: Dave, Rating: 2