9

INPUT

Dictionary 1

"a", "1"

"b", "2"

Dictionary 2

"a", "3"

"b", "4"

Dictionary 3

"a", "5"

"b", "6"

OUTPUT (Concatenation of the dictionaries above)

Final dictionary

"a", "9"

"b", "12"

I wrote a pseudo code for this :

  1. Create a Final empty dictionary.
  2. Loop over the list of dictionaries.
  3. Loop over the KeyValue pair.
  4. Check if the key exists in final dictionary. If yes then add the value from KeyValue pair to final dictionary. If not then add to dictionary the KeyValue pair

Since this requires two foreach loops is there a lync version in c# for this and also which doesn't throw any exception.

Some of the questions that i referred on stackoverflow was Combine multiple dictionaries into a single dictionary

Community
  • 1
  • 1
StackOverflowVeryHelpful
  • 2,347
  • 8
  • 34
  • 46
  • OK, so what code have you put together so far, and in what way didnt it work? – BugFinder Aug 21 '15 at 08:37
  • Dictionary finaldict = new Dictionary(); foreach (KeyValuePair> kvp in Dictionary) { foreach (KeyValuePair dict in kvp.Value.AsEnumerable()) { Class1 tempClass1; finaldict.TryGetValue(dict.Key, out tempClass1); if (tempClass1 != null) { tempClass1.Increment(dict.Value.Counter); } else { finaldict.Add(dict.Key, dict.Value); } } } – StackOverflowVeryHelpful Aug 21 '15 at 08:43
  • The code in comment above is just the core logic of the pseudo code i mentioned in the comment. – StackOverflowVeryHelpful Aug 21 '15 at 08:45
  • OK, but you've been here enough to know normally most people wont bother writing code for you without any shown effort – BugFinder Aug 21 '15 at 08:47
  • @BugFinder i completely agree with you and wasn't able to edit the question after my first edit. Once i am able to edit will add the code to the question. – StackOverflowVeryHelpful Aug 21 '15 at 08:55

2 Answers2

14
var dict1 = new Dictionary<string, int>() { { "a", 1 }, { "b", 2 } };
var dict2 = new Dictionary<string, int>() { { "a", 3 }, { "b", 4 } };
var dict3 = new Dictionary<string, int>() { { "a", 5 }, { "b", 6 } };

var resDict = dict1.Concat(dict2)
                   .Concat(dict3)
                   .GroupBy(x => x.Key)
                   .ToDictionary(x => x.Key, x => x.Sum(y=>y.Value));
Eser
  • 12,346
  • 1
  • 22
  • 32
  • This is the correct answer to the question that i asked for. Answer by @Martin below was working for any number of dictionaries so marked it as correct. Thanks Eser for taking your time to answer the question – StackOverflowVeryHelpful Aug 21 '15 at 09:10
12

You can use SelectMany to join all the key-value pairs from each dictionary into a single sequence that you then can group by key and sum the values for each key to determine the final value:

var dictionaries = new[] {
  new Dictionary<String, Int32>() { { "a", 1 }, { "b", 2 } },
  new Dictionary<String, Int32>() { { "a", 3 }, { "b", 4 } },
  new Dictionary<String, Int32>() { { "a", 5 }, { "b", 6 } }
};

var result = dictionaries
  .SelectMany(d => d)
  .GroupBy(
    kvp => kvp.Key,
    (key, kvps) => new { Key = key, Value = kvps.Sum(kvp => kvp.Value) }
  )
  .ToDictionary(x => x.Key, x => x.Value);

This solution works with any number of dictionaries - not just fixed set of dictionaries.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256