简体   繁体   English

如何比较KeyValuePairs的相等性?

[英]How are KeyValuePairs compared for equality?

I have two arrays of KeyValuePair<string, object> . 我有两个KeyValuePair<string, object>数组。 I am comparing the two arrays, but noticed that when the value of the KeyValuePair is a value type cast to an object, like object{int} , two pairs with identical keys and value values are not considered equal. 我正在比较两个数组,但是注意到,当KeyValuePair的值是KeyValuePair转换为对象(如object{int} )的值类型时,具有相同键和值的两对不视为相等。 However, when a reference type is cast to a value, like object{string} , two pairs with identical key and value values are considered equal. 但是,当将引用类型转换为值时,例如object{string} ,则将具有相同键值和值的两对视为相等。

I discovered this problem when I was using the .Except() method on the two arrays, and noticed although they were identical only the pairs with string values were removed by the set difference. 当我在两个数组上使用.Except()方法时,我发现了这个问题,并注意到尽管它们是相同的,但是只有字符串值对通过设置差异被删除。 From what I've seen doing research on the equality comparer for KeyValuePair , equality is by value, and not by reference. 从我看到的对KeyValuePair的相等比较器进行研究的结果来看,相等是按值而不是按引用。 Curiously, the reference for the pairs with string values should be different anyways. 奇怪的是,具有字符串值的对的引用无论如何都应该是不同的。

// array1 and array2 contain the same keys with identical values here

var array1 = GetOldItems(); // This returns a KeyValuePair<string, object>[]
var array2 = GetNewItems(); // This returns a KeyValuePair<string, object>[]
var diff = array1.Except(array2); // The difference is all pairs with non-string value

diff ends up being all pairs in array1 with a non-string value, even though all pairs in array1 had identical keys and values to the pairs in array2 . 即使array1所有对具有与array2的对相同的键和值, diff最终仍是array1中的所有对具有非字符串值。

EDIT: Here is the value of array1 and array2 as reported in the debugger: 编辑:这是在调试器中报告的array1array2的值:

{System.Collections.Generic.KeyValuePair<string, object>[5]}
[0]: {[Prop1, 1]}
[1]: {[Prop2, A]}
[2]: {[Prop2, B]}
[3]: {[Prop3, 3]}
[4]: {[Prop1, 2]}

Prop2 has keys where type of value is object{string} and the other pairs have values with type of object{int} . Prop2具有键,其中值的类型为object{string} ,而其他键对具有类型为object{int}

All pairs for Prop1 and Prop3 remain in the set difference, while all pairs with Prop2 have been removed. Prop1Prop3所有对保持设置的差异,而所有带有Prop2对都已删除。

KeyValuePair uses the default approach to struct equality. KeyValuePair使用默认方法进行结构相等。 If two KeyValuePair<TKey, TValue> have Key properties that compare as equal for their type's default equality and Value properties that compare as equal for their type's default equality, then they are equal. 如果两个KeyValuePair<TKey, TValue> Key属性的类型的默认相等性相等,而Value属性的类型的默认相等性相等,则它们相等。 Otherwise they are not. 否则他们不是。

I do not find what you report. 我找不到您报告的内容。 Eg: 例如:

new[] { new KeyValuePair<string, object>("a", 2) }.Except(
  new[] { (new KeyValuePair<string, object>("a", 2))})

returns an empty sequence, because the contents of the two arrays are identical. 返回空序列,因为两个数组的内容相同。

I would wonder if perhaps you have pairs where the values are some reference type that does not override Equals() and so are only considered equal if they are the same object. 我想知道您是否有对,其中值是某些引用类型,不会覆盖Equals() ,因此仅当它们是同一对象时才被视为相等。 In other words, that kvp1.Value.Equals(kvp2.Value) would return false. 换句话说,该kvp1.Value.Equals(kvp2.Value)将返回false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何返回IDictionary C#的所有KeyValuePairs - How to return all KeyValuePairs of a IDictionary C# 如何遍历字典中特定范围的 KeyValuePairs? - How to iterate through specific range of KeyValuePairs in a dictionary? 如何对包含一些操作的键值对列表进行排序 - How to sort a list of keyvaluepairs with some operations involved 如何使用键和值的Equals方法比较两个KeyValuePair? - How to compare two KeyValuePairs using the keys and values' Equals method? 如何使用LINQ通过匹配键和值来选择KeyValuePairs的列表 - How to Use LINQ to Select List of KeyValuePairs By Matching Key and Values 如何使用Serilog的ReadFrom.KeyValuePairs方法? - How to use Serilog's ReadFrom.KeyValuePairs method? C# MVC 如何使用 TemplateInfo 和 KeyValuePairs 初始化 ViewDataDictionary - C# MVC How to initialize ViewDataDictionary with both TemplateInfo and KeyValuePairs 如何将包含值(以逗号分隔的字符串)的Dictionary转换为KeyValuePairs的集合? - How to convert a Dictionary containing values as comma seperated strings into a Collection of KeyValuePairs? 如何保存列表<KeyValuePairs<int,object> &gt; 作为 SQLite 表? - How to save List<KeyValuePairs<int,object>> as a SQLite table? 如何检索Dictonary中包含的所有KeyValuePairs <?,?> ? - How can I retrieve all the KeyValuePairs contained in a Dictonary<?,?>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM