Solved

Append Dictionary onto an existing one

Posted on 2010-09-08
5
807 Views
Last Modified: 2013-12-17
Hi,

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)

0
Comment
Question by:darren-w-
5 Comments
 
LVL 53

Accepted Solution

by:
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)
        {
            Console.WriteLine(item.Key);
        }
0
 
LVL 5

Assisted Solution

by:LlamaJoe
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.
0
 
LVL 12

Assisted Solution

by:w00te
w00te earned 100 total points
ID: 33627200
From:
http://stackoverflow.com/questions/712927/how-to-add-2-dictionary-contents-without-looping-in-c

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

-w00te
0
 
LVL 12

Expert Comment

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

Author Comment

by:darren-w-
ID: 33627609
Ok thanks all
0

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