Chesnokov Yuriy
Chesnokov Yuriy

Reputation: 1838

Merging 2 dictionaries having duplicate keys with linq

How to merge 2 dictionaries of IDictionary<Guid, MyObject> where MyObject is a class instance?

IDictionary<Guid, MyObject> d1 = new Dictionary<Guid, MyObject>();
d1.Add(guid1, m1);
d1.Add(guid2, m2);
d1.Add(guid3, m3);
IDictionary<Guid, MyObject> d2 = new Dictionary<Guid, MyObject>();
d2.Add(guid2, m2);
d2.Add(guid3, m3);
d2.Add(guid4, m4);
IDictionary<Guid, MyObject> d3 = d1.Union(d2) ???

That in d3 there are the following entries:

guid1,m1
guid2,m2
guid3,m3
guid4,m4

Upvotes: 18

Views: 28053

Answers (7)

sibabratad
sibabratad

Reputation: 21

This will merge unique keys and unique list of values:

d1.Union(d2).GroupBy(g => g.Key).ToDictionary(pair => pair.Key, pair => pair.First().Value.Union(pair.Last().Value).ToList());

Upvotes: 2

user11441779
user11441779

Reputation:

If you have duplicate key then you'll have to handle duplicate key with the using of where clause.

var result = d1.Union(d2.Where(k => !d1.ContainsKey(k.Key))).ToDictionary(k => k.Key, v => v.Value)

Note : It will not get duplicate key. if there will be any duplicate key than it will get d1's key.

Upvotes: 4

Eric H
Eric H

Reputation: 1789

d1.Concat(d2.Where( x=> !d1.Keys.Contains(x.Key)));

Upvotes: 24

agent-j
agent-j

Reputation: 27923

d1.Union(d2).GroupBy (kvp => kvp.Key).ToDictionary(kvp => kvp.Key, kvp => kvp.First().Value); out to do the trick.

IDictionary<Guid, MyObject> d1 = new Dictionary<Guid, MyObject>();
d1.Add(guid1, m1);
d1.Add(guid2, m2);
d1.Add(guid3, m3);
IDictionary<Guid, MyObject> d2 = new Dictionary<Guid, MyObject>();
d2.Add(guid2, m2);
d2.Add(guid3, m3);
d2.Add(guid4, m4);
IDictionary<Guid, MyObject> d3 = 
   d1.Union(d2).GroupBy (kvp => kvp.Key)
       .ToDictionary (kvp => kvp.Key, kvp => kvp.First ().Value);

Upvotes: 7

Chaim Zonnenberg
Chaim Zonnenberg

Reputation: 1823

When no duplicates keys exist, the following works for 2 (or more) dictionaries:

var dictionaries = new [] { d1, d2 };
var result = dictionaries.SelectMany(dict => dict)
                     .ToDictionary(pair => pair.Key, pair => pair.Value);

Upvotes: -1

flq
flq

Reputation: 22849

You could try something like

d1.Concat(d2).Distinct(kv => kv.Key).ToDictionary(kv => kv.Key, kv => kv.Value)

The result of concat makes use of the fact that the dictionary is an IEnumerable<KeyvaluePair<Guid,MyObject>>

Since I do not have a compiler I just checked that Distinct cannot accept just a lambda selecting the property to be compared. However it can accept an EqualityComparer. What I often have in projects is a Generic Equality Comparer that allows to pass in lambdas which define the equality operation.

Upvotes: 0

Related Questions