I have two dictionaries of type <string,object> in C#.
How can I copy all the contents of one Dictionary object to the other without applying a loop?
- 20,816
- 57
- 73
- 92
-
The type name in your question seems to have gone AWOL. – dommer Apr 03 '09 at 07:51
-
if you're looking for a merge you should refer to: http://stackoverflow.com/questions/294138/merging-dictionaries-in-c – annakata Apr 03 '09 at 07:57
4 Answers
var d3 = d1.Concat(d2).ToDictionary(x => x.Key, x => x.Value);
- 837,282
- 173
- 1,862
- 1,933
- 3,459
- 3
- 32
- 41
-
-
Sorry but I posted before bruno updated his answer to include the ToDictionary call - previously the code was returning an IEnumerable
– Gordon Mackie JoanMiro Apr 03 '09 at 10:54 -
Oh great! So I get voted down because bruno went back and updated his answer AFTER mine! – Gordon Mackie JoanMiro Apr 03 '09 at 10:58
-
2
-
13I seem to recall that his edit to add the ToDictionary call happened an hour after my post - but what the heck - I've calmed down now and it ain't the end of the world :-) – Gordon Mackie JoanMiro Apr 06 '09 at 08:19
-
I upvoted you man. I've been burned by being a few seconds too late in the past, so I feel your pain. (I also upvoted Bruno as well.) :-) – Pretzel Apr 19 '10 at 13:59
You can use Concat:
Dictionary<string, object> d1 = new Dictionary<string, object>();
d1.Add("a", new object());
d1.Add("b", new object());
Dictionary<string, object> d2 = new Dictionary<string, object>();
d2.Add("c", new object());
d2.Add("d", new object());
Dictionary<string, object> d3 = d1.Concat(d2).ToDictionary(e => e.Key, e => e.Value);
foreach (var item in d3)
{
Console.WriteLine(item.Key);
}
- 28,235
- 15
- 95
- 135
- 47,131
- 14
- 97
- 117
-
1This will return an IEnumerable
>, you will need to call ToDictionary to get a dictionary. – Richard Szalay Apr 03 '09 at 08:45
First up, it's not possible without looping. Whether that loop is done in a (extension) method is irrelevent, it still requires a loop.
I'm actually going to recommend doing it manually. All the other answers given require using two extention methods (Concat - ToDictionary and SelectMany - ToDictionary) and thus looping twice. If you are doing this to optimise your code, it will be faster to do a loop over dictionary B and add it's contents to dictionary A.
Edit: After further investigation, the Concat operation would only occur during the ToDictionary call, but I still think a custom extension method would be more efficient.
If you want to reduce your code size, then just make an extension method:
public static class DictionaryExtensions
{
public static IDictionary<TKey,TVal> Merge<TKey,TVal>(this IDictionary<TKey,TVal> dictA, IDictionary<TKey,TVal> dictB)
{
IDictionary<TKey,TVal> output = new Dictionary<TKey,TVal>(dictA);
foreach (KeyValuePair<TKey,TVal> pair in dictB)
{
// TODO: Check for collisions?
output.Add(pair.Key, Pair.Value);
}
return output;
}
}
Then you can use it by importing ('using') the DictionaryExtensions namespace and writing:
IDictionary<string,objet> output = dictA.Merge(dictB);
I have made the method act like the objects are immutable, but you could easily modify it to not return a new dictionary and just merge into dictA.
- 81,010
- 19
- 172
- 232
var result = dictionaries.SelectMany(dict => dict)
.ToDictionary(pair => pair.Key, pair => pair.Value);
- 36,352
- 14
- 132
- 118