There are a few pieces to doing this, assuming each list does not contain duplicates, Name is a unique identifier, and neither list is ordered.
First create an append extension method to get a single list:
static class Ext {
public static IEnumerable<T> Append(this IEnumerable<T> source,
IEnumerable<T> second) {
foreach (T t in source) { yield return t; }
foreach (T t in second) { yield return t; }
}
}
Thus can get a single list:
var oneList = list1.Append(list2);
Then group on name
var grouped = oneList.Group(p => p.Name);
Then can process each group with a helper to process one group at a time
public Person MergePersonGroup(IGrouping<string, Person> pGroup) {
var l = pGroup.ToList(); // Avoid multiple enumeration.
var first = l.First();
var result = new Person {
Name = first.Name,
Value = first.Value
};
if (l.Count() == 1) {
return result;
} else if (l.Count() == 2) {
result.Change = first.Value - l.Last().Value;
return result;
} else {
throw new ApplicationException("Too many " + result.Name);
}
}
Which can be applied to each element of grouped
:
var finalResult = grouped.Select(g => MergePersonGroup(g));
(Warning: untested.)