好吧,这是一个尝试,假设
first
和
second
是有问题的词典。
var items = from key in first.Keys.Concat(second.Keys).Distinct()
let firstList = first.GetValueOrDefault(key) ?? new List<string>()
let secondList = second.GetValueOrDefault(key) ?? new List<string>()
select new { Key = key,
Additional = secondList.Except(firstList),
Missing = firstList.Except(secondList) };
var result = items.ToDictionary(x => x.Key,
x => new MyClass(x.Additional, x.Missing));
public static TValue GetValueOrDefault<TKey, TValue>
(this IDictionary<TKey, TValue> dictionary,
TKey key)
{
TValue value;
dictionary.TryGetValue(key, out value)
return value;
}