468,765 Members | 1,076 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

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 6641
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Brian P | last post: by
1 post views Thread by Eran | last post: by
7 posts views Thread by Andrew Robinson | last post: by
4 posts views Thread by Peter K | last post: by
4 posts views Thread by Mark S. | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.