locked
Comparing Two Dictionaries and creating new dictionaries with values alone RRS feed

  • Question

  • User-1446050902 posted

    I have two dictionaries Dic A &  Dic B. Dic A & Dic B have same keys . I would like to move the values in both the dictionaries into a new dictionary (with Value from Dic A being the key in new dictionary).  I couldnt figure out a way in merging or intersecting the two dictionaries to get a new one. What would be the right approach in getting the desired output

    Dic A

    =======

    A1   Val 1

    A2   Val 2

    A3   Val 3

    Dic B

    ========

    A1  Cat 1

    A2  Cat 2 

    A3  Cat 3

    Desired Result

    Dic C

    =======

    Val 1  Cat 1 

    Val 2  Cat 2 

    Val 3  Cat 3

    Tuesday, August 5, 2014 6:58 AM

Answers

  • User281315223 posted

    You could probably use the available LINQ ToDictionary() method and map the values from your dicA Dictionary to the values for the same key in your second dicB dictionary :

    // Build your first Dictionary
    var dicA = new Dictionary<string,string>();
    dicA.Add("A1","Val 1");
    dicA.Add("A2","Val 2");
    dicA.Add("A3","Val 3");
    		
    // Build your second Dictionary
    var dicB = new Dictionary<string,string>();
    dicB.Add("A1","Cat 1");
    dicB.Add("A2","Cat 2");
    dicB.Add("A3","Cat 3");
    		
    // Merge your two dictionaries (mapping Values from the First to Keys from the second)
    var dicC = dicA.ToDictionary(k => k.Value, k => dicB[k.Key]);
    

    You can see a working example here. Additionally, you'll likely want to check to ensure that you actually have a key that corresponds to each of your values within dicB for this to consistently work. This could be done via a where clause as seen below :

    // Merge your two dictionaries (mapping Values from the First to Keys from the second)
    var dicC = dicA.Where(d => dicB.ContainsKey(d.Key)).ToDictionary(k => k.Value, k => dicB[k.Key]);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 5, 2014 11:23 AM

All replies

  • User1428246847 posted

    dotnet_rookie

    What would be the right approach in getting the desired output

    Not sure if it's the right approach, but it's one that works

                Dictionary<string, string> dicC = new Dictionary<string, string>();
                foreach (KeyValuePair<string,string> kvp in dicA)
                {
                    dicC.Add(kvp.Value, dicB[kvp.Key]);
                }

    PS: there is no checking if the key in dicA indeed exists in dicB; I leave that up to you ;)

    Tuesday, August 5, 2014 10:48 AM
  • User281315223 posted

    You could probably use the available LINQ ToDictionary() method and map the values from your dicA Dictionary to the values for the same key in your second dicB dictionary :

    // Build your first Dictionary
    var dicA = new Dictionary<string,string>();
    dicA.Add("A1","Val 1");
    dicA.Add("A2","Val 2");
    dicA.Add("A3","Val 3");
    		
    // Build your second Dictionary
    var dicB = new Dictionary<string,string>();
    dicB.Add("A1","Cat 1");
    dicB.Add("A2","Cat 2");
    dicB.Add("A3","Cat 3");
    		
    // Merge your two dictionaries (mapping Values from the First to Keys from the second)
    var dicC = dicA.ToDictionary(k => k.Value, k => dicB[k.Key]);
    

    You can see a working example here. Additionally, you'll likely want to check to ensure that you actually have a key that corresponds to each of your values within dicB for this to consistently work. This could be done via a where clause as seen below :

    // Merge your two dictionaries (mapping Values from the First to Keys from the second)
    var dicC = dicA.Where(d => dicB.ContainsKey(d.Key)).ToDictionary(k => k.Value, k => dicB[k.Key]);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 5, 2014 11:23 AM