473,245 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,245 software developers and data experts.

Concat two Dictionary<>

Hi,

How do i concat two dictionaries aka the following sample:

Dictionary<int, stringa;
Dictionary<int, stringb;
b.Add(a);

What is the easiest way to concat two dictionaries ??

BR
Peter
Apr 1 '08 #1
8 7074
"Peter Larsen [CPH]" <Pe*********@community.nospamwrote:
How do i concat two dictionaries aka the following sample:
Dictionary<int, stringa;
Dictionary<int, stringb;
What do you want to do if both dictionaries use the same integer key?
(For example, do you want to produce an <int, string[]?)

If you know that won't happen, then just use a loop and add each element
of one to the other.

Eq.
Apr 1 '08 #2
Are their any potential clashes in the key? In other words, suppose the
following:

Dictionary<int, stringa = new Dictionary<int, string>();
Dictionary<int, stringb = new Dictionary<int, string>();

a.Add(1,"a");
a.Add(2,"b");

b.Add(1, "c");
b.Add(2, "d");

You have a clash here. Although you can concat in one statement, in
Framework 3.5, you will end up with a clash in keys, which is not good. If b
is filled like so:

b.Add(3, "c");
b.Add(4, "d");

you do not end up with the same issue.

If you have clashes, you will have to loop.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

*************************************************
| Think outside the box!
|
*************************************************
"Peter Larsen [CPH]" <Pe*********@community.nospamwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi,

How do i concat two dictionaries aka the following sample:

Dictionary<int, stringa;
Dictionary<int, stringb;
b.Add(a);

What is the easiest way to concat two dictionaries ??

BR
Peter

Apr 1 '08 #3
I am aware about the potential danger in the sample - i'm just trying to
figure out what linq and lambda is and what it can do for me :-)

/Peter

"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamMwrote in
message news:Ob*************@TK2MSFTNGP06.phx.gbl...
Are their any potential clashes in the key? In other words, suppose the
following:

Dictionary<int, stringa = new Dictionary<int, string>();
Dictionary<int, stringb = new Dictionary<int, string>();

a.Add(1,"a");
a.Add(2,"b");

b.Add(1, "c");
b.Add(2, "d");

You have a clash here. Although you can concat in one statement, in
Framework 3.5, you will end up with a clash in keys, which is not good. If
b is filled like so:

b.Add(3, "c");
b.Add(4, "d");

you do not end up with the same issue.

If you have clashes, you will have to loop.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

Apr 1 '08 #4
I just want to copy the items from dictionary "a" to dictionary "b".

Normally i would do something similar to this:
Dictionary<int, stringb = new Dictionary<int, string>(a);

I'm just trying to find new ways using linq...

/Peter

>
>Is it possible to do something similar to the following:

Dictionary<int, stringa = ... {add some some items};
Dictionary<int, stringb = from item in a select ... what to do here
??

It's not entirely clear to me what you actually want to do.

Apr 1 '08 #5
write an extension method!

of course if you could write static extensions you could do...

public static Concat(ref this null, Dictionary<t, kdict1, Dictionry<t, k>
dict2)
{
...
}

Dictionary<int, stringa;
Dictionary<int, stringb;
....
Dictionary<int, stringc = Dictionary.Concat(a, b);

but no one seems to agree with me about grouping related functions with
static extensions!

Dict
"Peter Larsen [CPH]" wrote:
Hi,

How do i concat two dictionaries aka the following sample:

Dictionary<int, stringa;
Dictionary<int, stringb;
b.Add(a);

What is the easiest way to concat two dictionaries ??

BR
Peter
Apr 1 '08 #6
Thank you for you comments.
I think my question has been answered by now.

/Peter

Apr 2 '08 #7
One of the best, human readible that is, explanations of Lambda expressions
is found in the book Introducing Microsoft(r) LINQ. You can read Chapter 2
from the free book offer page (http://csna01.libredigital.com/).

It runs through Generics to Delegates to Anonymous Methods to Extension
Methods to Lambda Expressions. Once you follow the path, you should have a
decent idea of how to navigate this jungle.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

*************************************************
| Think outside the box!
|
*************************************************
"Peter Larsen [CPH]" <Pe*********@community.nospamwrote in message
news:Os**************@TK2MSFTNGP05.phx.gbl...
>I am aware about the potential danger in the sample - i'm just trying to
figure out what linq and lambda is and what it can do for me :-)

/Peter

"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamMwrote in
message news:Ob*************@TK2MSFTNGP06.phx.gbl...
>Are their any potential clashes in the key? In other words, suppose the
following:

Dictionary<int, stringa = new Dictionary<int, string>();
Dictionary<int, stringb = new Dictionary<int, string>();

a.Add(1,"a");
a.Add(2,"b");

b.Add(1, "c");
b.Add(2, "d");

You have a clash here. Although you can concat in one statement, in
Framework 3.5, you will end up with a clash in keys, which is not good.
If b is filled like so:

b.Add(3, "c");
b.Add(4, "d");

you do not end up with the same issue.

If you have clashes, you will have to loop.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/


Apr 3 '08 #8
Thanks for the links Gregory..

/Peter

"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamMwrote in
message news:u0*************@TK2MSFTNGP05.phx.gbl...
One of the best, human readible that is, explanations of Lambda
expressions is found in the book Introducing Microsoft(r) LINQ. You can
read Chapter 2 from the free book offer page
(http://csna01.libredigital.com/).

It runs through Generics to Delegates to Anonymous Methods to Extension
Methods to Lambda Expressions. Once you follow the path, you should have a
decent idea of how to navigate this jungle.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/
Apr 4 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ESPNSTI | last post by:
Hi, I'm trying to use a generics dictionary with a key class that implements and needs IComparable<>. However when I attempt to use the dictionary, it doesn't appear to use the IComparable<> to...
8
by: Brian P | last post by:
I want to expose a property of Dictionary<string, MyAbstractClass>. I tried to do it this way: private Dictionary<string, MyChildClass> _dictionary; public Dictionary<string,...
1
by: Eran | last post by:
Hi, I have a huge data structure, which I previosly stored in a Dictionary<int, MyObj> MyObj is relatively small (2 int, 1 DateTime, 1 bool). The dictionary I am using is quite large...
7
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
4
by: Peter K | last post by:
Hi are there any benefits in using StringDictionary over Dictionary<string, string? It appears they achieve the same thing... (I could be wrong of course). thanks, Peter
4
by: Mark S. | last post by:
Hello, I have a series of changing string IDs that are loaded dynamically a couple times a minute. I need to associate each ID with a different static class so later on in the app's lifecycle it...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.