c# - Merge Dictionary<TKey, TValue> with Enumerable.Union method -


i'm testing union method merge dictionaries (of type dictionary). works fine tvalue type string or int or object. if tvalue type collection (tested list , object[]) exception thrown : "argumentexception: item same key has been added."

here code :

dictionary<int,string> _dico1 = new dictionary<int, string>() {     {0, "zero"},     {1, "one"} };  dictionary<int,string> _dico2 = new dictionary<int,string>() {     {1 , "one"},     {2 , "two"},     {3 , "three"},     {4 , "four"},     {5 , "five"},     {6 , "six"} };  dictionary<int, list<string>> _dico3 = new dictionary<int, list<string>>() {     {0, new list<string>{"zero"}},     {1, new list<string>{"one"}} };  dictionary<int, list<string>> _dico4 = new dictionary<int, list<string>>() {     {1, new list<string>{"one"}},     {2, new list<string>{"two"}},     {3, new list<string>{"three"}},     {4, new list<string>{"four"}},     {5, new list<string>{"five"}},     {6, new list<string>{"six"}}, };      // works fine     var mergedico = _dico1.union(_dico2).todictionary(key => key.key, value => value.value);      // throw argumentexception : item same key has been added     var mergedico2 = _dico3.union(_dico4).todictionary(key => key.key, value => value.value); 

why behavior not same ? , how resolve problem ?

thank !

in first case, union discarding duplicate keys because key/value pairs equal. in second case they're not, because list<string>{"one"} isn't equal list<string>{"one"}.

i suspect want union call use iequalitycomparer takes account of keys within dictionary.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -