Пользовательский IEqualityComparer не возвращает ожидаемый результат ⇐ C#
Пользовательский IEqualityComparer не возвращает ожидаемый результат
Looked everywhere and cannot find a solution which works. Need urgent help as every solution I have found does not seems to work for my situation.
public class KeyValuePairComparer : IEqualityComparer { public bool Equals(KeyValuePair x, KeyValuePair y) { return EqualityComparer.Default.Equals(x.Key, y.Key) && EqualityComparer.Default.Equals(x.Value, y.Value); } public int GetHashCode(KeyValuePair obj) { return EqualityComparer.Default.GetHashCode(obj.Key) ^ EqualityComparer.Default.GetHashCode(obj.Value); } } public static IEnumerable GetDifferences( Dictionary x, Dictionary y) { return x.Except(y, new KeyValuePairComparer()); } Dictionary dict1 = new Dictionary(){ {"Key1","1"}, {"Key2",2}, {"Key3","4"}, {"Key4",3}, } Dictionary dict2 = new Dictionary(){ {"Key1","1"}, {"Key2",2}, {"Key3","3"}, {"Key4",4}, } List differences = GetDifferences(dict1,dict2).ToList(); Output would be
Dictionary(){ {"Key3","4"}, {"Key4",3} }
Источник: https://stackoverflow.com/questions/781 ... ted-result
Looked everywhere and cannot find a solution which works. Need urgent help as every solution I have found does not seems to work for my situation.
public class KeyValuePairComparer : IEqualityComparer { public bool Equals(KeyValuePair x, KeyValuePair y) { return EqualityComparer.Default.Equals(x.Key, y.Key) && EqualityComparer.Default.Equals(x.Value, y.Value); } public int GetHashCode(KeyValuePair obj) { return EqualityComparer.Default.GetHashCode(obj.Key) ^ EqualityComparer.Default.GetHashCode(obj.Value); } } public static IEnumerable GetDifferences( Dictionary x, Dictionary y) { return x.Except(y, new KeyValuePairComparer()); } Dictionary dict1 = new Dictionary(){ {"Key1","1"}, {"Key2",2}, {"Key3","4"}, {"Key4",3}, } Dictionary dict2 = new Dictionary(){ {"Key1","1"}, {"Key2",2}, {"Key3","3"}, {"Key4",4}, } List differences = GetDifferences(dict1,dict2).ToList(); Output would be
Dictionary(){ {"Key3","4"}, {"Key4",3} }
Источник: https://stackoverflow.com/questions/781 ... ted-result
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение