How do I sum values from two dictionaries in C#?

The following isn't the most efficient solution (because it simply treats both dictionaries as enumerables), but it will work and it is quite clear: Dictionary result = (from e in foo. Concat(bar) group e by e. Key into g select new { Name = g.

Key, Count = g. Sum(kvp => kvp. Value) }) .

ToDictionary(item => item. Name, item => item. Count).

Ben: Thanks for the correction, I just realized that too. – Tomas Petricek May 11 '10 at 16:40 Edited your answer to show how to get the resulting query back into a dictionary. – George Stocker May 11 '10 at 16:47 @George: Thanks – Tomas Petricek May 11 '10 at 16:50 More Clear example.

– Mohanavel May 11 '10 at 16:56.

(from a in foo join be in bar on a. Key equals b. Key select new { Key = a.

Key, Value = a. Value + b. Value }) .

ToDictionary(a => a. Key,a => a. Value) That should do it.

EDIT: Might be more efficient (not sure how the join is implemented) (from a in foo let be = bar. ContainsKey(a. Key)?(int?)bara.

Key : null select new { Key = a. Key, Value = a. Value + (b!

= null? Be : 0) } ). ToDictionary(a => a.

Key, a => a. Value).

Thanks for the answer; this answer also helps if you want to compute deltas between the objects. – George Stocker May 11 '10 at 17:48.

Whats wrong with foreach (string key in d1. Keys) { d3. Add(key,d1key+d2key); }?

I actually think its more clear than some of the linq solutions. Even though I haven't tested it, I think it could have better performance, since it only enumerates the keys in one dictionary and not the values, you'd use the actual hashing (or whatever is the underlying implementation of the dictionary) to find the values, which is the fastest way to get them. EDIT: for the solution where keys wouldnt always be the same, if you only want to get shared ones,you only need to add a line; foreach (string key in d1.

Keys) { if(d2. ContainsKey(key) d3. Add(key,d1key+d2key); } EDIT2: In order to get all keys/values if they are not the same, then it'd be like this: foreach (string key in d1.

Keys) { if(d2. ContainsKey(key) d3. Add(key,d1key+d2key); else d3.

Add(key,d1key) } foreach (string key in d2. Keys) { if(!d1. ContainsKey(key) // only get keys that are unique to d2 d3.

Add(key,d2key); }.

Well, there's still the matter of d2 having keys that aren't in d1, of course... – Dan Tao May 11 '10 at 17:51 @Dan Tao yeah, that would only work for the shared keys. EDIT: okay I added the solution for that :P – Francisco Noriega May 11 '10 at 17:54.

If you have a cast iron guarantee that the two sets of keys are the same: Dictionary Res2 = foo. ToDictionary(orig => orig. Key, orig => orig.

Value + barorig. Key); Best I could come up with if keys aren't same set: var AllKeys = foo.Keys. Union(bar.

Keys); var res3 = AllKeys. ToDictionary(key => key, key => (foo.Keys. Contains(key)?

Fookey : 0) + (bar.Keys. Contains(key)? Barkey : 0)).

Var fooBar = foo. Keys . Union(bar.

Keys) . Select( key => { int fval = 0, bval = 0; foo. TryGetValue(key, out fval); bar.

TryGetValue(key, out bval); return new KeyValuePair(key, fval + bval); } ) . ToDictionary(kvp => kvp. Key, kvp => kvp.

Value); At least it's (kind of? ) neat.

Select new { Key = a. Key, Value = a. Value + b.

ToDictionary(a => a. Key,a => a. Let be = bar.

Select new { Key = a. Key, Value = a. Value + (b!

ToDictionary(a => a. Key, a => a.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions