Solved

Append Dictionary onto an existing one

Posted on 2010-09-08
5
817 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

815 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

8 Experts available now in Live!

Get 1:1 Help Now