[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Append Dictionary onto an existing one

Posted on 2010-09-08
5
Medium Priority
?
851 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 1200 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 400 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 400 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Loops Section Overview
Suggested Courses

873 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