Append Dictionary onto an existing one

Posted on 2010-09-08
Last Modified: 2013-12-17

I have an existing dictionary variable:

      Dictionary<string, string> itemList = new Dictionary<string, string> ();

And I would like to add the below one to it so the values are added to the end, its returned from a method.

       Dictionary<string, string> returnValue = new Dictionary<string, string> ();

ie somthing like:  itemList +=returnValue (I know this is wrong)

Question by:darren-w-
LVL 53

Accepted Solution

Dhaest earned 300 total points
ID: 33626924
You can the concat

Dictionary<string, string> d3 = d1.Concat(d2).ToDictionary(e => e.Key, e => e.Value);
        foreach (var item in d3)

Assisted Solution

LlamaJoe earned 100 total points
ID: 33627063
using LINQ you can join into a new variable, and then assign it back to the original variable.

                    Dictionary<string, string> itemList = new Dictionary<string, string> ();
                    itemList.Add("a", "b");
                    Dictionary<string, string> returnValue = new Dictionary<string, string> ();
                    returnValue.Add("c", "d");

                    var newList = itemList.Concat(returnValue);

Dictionary is not a sealed class, so you could override the Add method to make a method to allow you to add another dictionary and return the result.
LVL 12

Assisted Solution

w00te earned 100 total points
ID: 33627200

d1.Concat(d2).ToDictionary(x => x.Key, x => x.Value);

LVL 12

Expert Comment

ID: 33627208
Actually mine's prety much the same as Dhaest, go for his.  Sorry about that :)
LVL 13

Author Comment

ID: 33627609
Ok thanks all

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Loop not working 29 47
Help with WinAPI Signatures in VB.NET for SetWindowSubclass 2 34
C# Single Form 8 26
Automated testing suggestions? 2 18
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now