Fusionar diccionario <TKey, TValue> con método Enumerable.Union
Estoy probando elUNIÓN método para fusionar a diccionarios (de tipo Diccionario). Funciona bien con el tipo TValue es string o int o incluso objeto. Pero si el tipo TValue es una colección (probado con List y object []) se genera una excepción:"ArgumentException: ya se agregó un elemento con la misma clave".
Aquí está mi código:
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 an ArgumentException : An item with the same key has already been added
var mergeDico2 = _dico3.Union(_dico4).ToDictionary(key => key.Key, value => value.Value);
¿Por qué el comportamiento no es el mismo? ¿Y cómo resolver este problema?
Gracias !