Concat two Dictionary<>

  • Thread starter Thread starter Peter Larsen [CPH]
  • Start date Start date
P

Peter Larsen [CPH]

Hi,

How do i concat two dictionaries aka the following sample:

Dictionary<int, string> a;
Dictionary<int, string> b;
b.Add(a);

What is the easiest way to concat two dictionaries ??

BR
Peter
 
Peter Larsen said:
How do i concat two dictionaries aka the following sample:

Dictionary<int, string> a;
Dictionary<int, string> b;
b.Add(a);

What is the easiest way to concat two dictionaries ??

foreach (KeyValuePair<int,string> pair in b)
{
a.Add(pair.Key, pair.Value);
}

Or if you're using .NET 3.5 you could consider creating a new
dictionary instead, using something like:

var combined = a.Concat(b)
.ToDictionary(pair => pair.Key, pair => pair.Value);
 
Are their any potential clashes in the key? In other words, suppose the
following:

Dictionary<int, string> a = new Dictionary<int, string>();
Dictionary<int, string> b = 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!
|
*************************************************
 
Hi Jon,
I am using framework 3.5.

So if i don't care about the type of the anonymous type, i could simply
write:
var combined = a.Concat(b)

Is it possible to do something similar to the following:

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

/Peter
 
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
 
Peter Larsen said:
So if i don't care about the type of the anonymous type, i could simply
write:
var combined = a.Concat(b)

That won't have an anonymous type at all - but it'll just be an
IEnumerable said:
Is it possible to do something similar to the following:

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

It's not entirely clear to me what you actually want to do.
 
I just want to copy the items from dictionary "a" to dictionary "b".

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

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

/Peter
 
Peter Larsen said:
I just want to copy the items from dictionary "a" to dictionary "b".

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

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

LINQ isn't going to give you anything simpler than that - a single call
to a constructor.
 
write an extension method!

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

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

Dictionary<int, string> a;
Dictionary<int, string> b;
....
Dictionary<int, string> c = Dictionary.Concat(a, b);

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

Dict
 
Jarlaxle said:
write an extension method!

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

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

Dictionary<int, string> a;
Dictionary<int, string> b;
...
Dictionary<int, string> c = Dictionary.Concat(a, b);

How is that significantly clearer than either of:

Dictionary<int, string> c = DictionaryUtil.Concat(a, b);
or
Dictionary<int, string> c = a.Concat(b);

?
 
How is that significantly clearer than either of:
Dictionary<int, string> c = DictionaryUtil.Concat(a, b);
or
Dictionary<int, string> c = a.Concat(b);

i agree with you there are workarounds. i personally hate having helper
classes and would rather keep them encapsulated in one object and yes in this
case you can have a.method(b)

but consider RegistryKey for example...

I' like to add a DeleteSubKeyTree without throwing an exception so i don't
have to wrap all my calls in exception handlers.

or add a RegistryKey.IsKeyPresent...registryKey.IsValuePresent

or add some methods on the File class and/or Environment class.

yes we can have a FileHelper or FileUtil class but always remember where
your method is!

Just thought it would be nice.
 
Just thought it would be nice.

Remember that the bar for including a language feature is pretty high.
There has to be a really pretty significant benefit before it will make
it in there - the costs of language changes are high, in terms of:

1) Educating developers
2) Specifying the feature (without making the language much more
complicated)
3) Changing the compiler
4) Lots of documentation
.... etc

Would it be handy occasionally? Maybe.
Is that a good enough reason? Not in my view.
 
Thank you for you comments.
I think my question has been answered by now.

/Peter
 
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!
|
*************************************************
 
This worked for me, considering there are no duplicated values:

using System.Collections.Generic;
using System.Linq;

Dictionary<int, string> a;
Dictionary<int, string> b;
a = a.Concat(b).ToDictionary(v => v.Key, v => v.Value);

LKZ:

Hi,

How do i concat two dictionaries aka the following sample:

Dictionary<int, string> a;
Dictionary<int, string> b;
b.Add(a);

What is the easiest way to concat two dictionaries ??

BR
Peter
On Tuesday, April 01, 2008 9:11 AM Jon Skeet [C# MVP] wrote:

foreach (KeyValuePair<int,string> pair in b)
{
a.Add(pair.Key, pair.Value);
}

Or if you're using .NET 3.5 you could consider creating a new
dictionary instead, using something like:

var combined = a.Concat(b)
.ToDictionary(pair => pair.Key, pair => pair.Value);
On Tuesday, April 01, 2008 9:12 AM Paul E Collins wrote:


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.
On Tuesday, April 01, 2008 9:59 AM Peter Larsen [CPH] wrote:
Hi Jon,
I am using framework 3.5.

So if i don't care about the type of the anonymous type, i could simply
write:
var combined = a.Concat(b)

Is it possible to do something similar to the following:

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

/Peter
On Tuesday, April 01, 2008 10:03 AM Peter Larsen [CPH] wrote:
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

message news:[email protected]...
On Tuesday, April 01, 2008 10:06 AM Jon Skeet [C# MVP] wrote:

That won't have an anonymous type at all - but it'll just be an
IEnumerable<KeyValuePair<int,string>>; it won't be a dictionary.


It's not entirely clear to me what you actually want to do.
On Tuesday, April 01, 2008 10:25 AM Peter Larsen [CPH] wrote:
I just want to copy the items from dictionary "a" to dictionary "b".

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

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

/Peter
On Tuesday, April 01, 2008 10:40 AM Jon Skeet [C# MVP] wrote:

LINQ isn't going to give you anything simpler than that - a single call
to a constructor.
On Tuesday, April 01, 2008 10:53 AM Jarlaxl wrote:
write an extension method!

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

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

Dictionary<int, string> a;
Dictionary<int, string> b;
...
Dictionary<int, string> c = Dictionary.Concat(a, b);

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

Dict


"Peter Larsen [CPH]" wrote:
On Tuesday, April 01, 2008 10:59 AM Jon Skeet [C# MVP] wrote:

How is that significantly clearer than either of:

Dictionary<int, string> c = DictionaryUtil.Concat(a, b);
or
Dictionary<int, string> c = a.Concat(b);

?
On Tuesday, April 01, 2008 11:41 AM Jarlaxl wrote:
i agree with you there are workarounds. i personally hate having helper
classes and would rather keep them encapsulated in one object and yes in this
case you can have a.method(b)

but consider RegistryKey for example...

I' like to add a DeleteSubKeyTree without throwing an exception so i don't
have to wrap all my calls in exception handlers.

or add a RegistryKey.IsKeyPresent...registryKey.IsValuePresent

or add some methods on the File class and/or Environment class.

yes we can have a FileHelper or FileUtil class but always remember where
your method is!

Just thought it would be nice.




"Jon Skeet [C# MVP]" wrote:
On Tuesday, April 01, 2008 11:47 AM Jon Skeet [C# MVP] wrote:

<snip>


Remember that the bar for including a language feature is pretty high.
There has to be a really pretty significant benefit before it will make
it in there - the costs of language changes are high, in terms of:

1) Educating developers
2) Specifying the feature (without making the language much more
complicated)
3) Changing the compiler
4) Lots of documentation
... etc

Would it be handy occasionally? Maybe.
Is that a good enough reason? Not in my view.
On Wednesday, April 02, 2008 4:48 AM Peter Larsen [CPH] wrote:
Thank you for you comments.
I think my question has been answered by now.

/Peter
On Friday, April 04, 2008 2:53 AM Peter Larsen [CPH] wrote:
Thanks for the links Gregory..

/Peter

"Cowboy (Gregory A. Beamer)" <[email protected]> wrote in
 
Back
Top