Home > Net >  How Can I merge two Dictionaries without getting Argument exception on the Key
How Can I merge two Dictionaries without getting Argument exception on the Key

Time:11-20

I can't figure out how to keep the keys and values on a dictionary when I try to merge two dictionaries. I keep getting argument Exception due to duplicate of key. When the key match i just like to add the value by = kvp.value;

I have a list of Dictionaries where the 1st Dictionary = kvp = "jump", 2; 2ndDictionary = kvp = "jump", 4;

I like to merge them and get something like: Dictionary = kvp = "jump", 6;

That I can later add to my list of Dictionaries

I've tried to run something i found in stackoverflow thread.

foreach (var dict in listOfDict)
{
         dict.SelectMany(d => d)
            .ToLookup(pair => pair.Key, pair => pair.Value)
            .ToDictionary(group => group.Key, group => group.First());
}

But I keep getting.

cannot be inferred from the usage. Try specifying the type arguments explicitly.

I want to avoid getting all keys and all values on separate lists that i later loop through to add key and value on a new dictionary.

CodePudding user response:

If you are getting an exception due to duplicate keys, then it sounds like you have duplicate keys!

Have you checked the two dictionaries before you try to merge them? Simply calling = kvp.value without checking to see if the first dictionary already has a key of that name is very likely to be your problem.

You need to check for an existing entry with that key, and if one is found, take whatever action is appropriate for your scenario (ie ignore, overwrite, ask the user to decide, etc)

CodePudding user response:

If you like the LINQ approach, I would go with something like this:

    var dictionaries = new List<Dictionary<string, int>>(); // this is the list of dictionaries you want to merge
    var unifiedDictionary = new Dictionary<string, int>(); // this is the dictionary where you merge and add the values

    foreach (var kvp in dictionaries.SelectMany(dictionary => dictionary))
    {
        if (unifiedDictionary.ContainsKey(kvp.Key))
        {
            unifiedDictionary[kvp.Key]  = kvp.Value;
        }
        else
        {
            unifiedDictionary.Add(kvp.Key, kvp.Value);
        }
    }

However, if this is too hard to read (I am not always a fan of excessive LINQ over explicit code blocks), you can use the for-loop approach:

    var dictionaries = new List<Dictionary<string, int>>(); // this is the list of dictionaries you want to merge
    var unifiedDictionary = new Dictionary<string, int>(); // this is the dictionary where you merge and add the values

    foreach (var dictionary in dictionaries)
    {
        foreach (var kvp in dictionary)
        {
            if (unifiedDictionary.ContainsKey(kvp.Key))
            {
                unifiedDictionary[kvp.Key]  = kvp.Value;
            }
            else
            {
                unifiedDictionary.Add(kvp.Key, kvp.Value);
            }
        }
    }

Hope this helps you. If further help and explanations are needed, please tell me.

  • Related