.NET Forum / Languages / C# / April 2008
Concat two Dictionary<>
|
|
Thread rating:  |
Peter Larsen [CPH] - 01 Apr 2008 14:00 GMT 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
Jon Skeet [C# MVP] - 01 Apr 2008 14:11 GMT > How do i concat two dictionaries aka the following sample: > [quoted text clipped - 3 lines] > > 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);
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
Peter Larsen [CPH] - 01 Apr 2008 14:59 GMT 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
> 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); Jon Skeet [C# MVP] - 01 Apr 2008 15:06 GMT > 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<KeyValuePair<int,string>>; it won't be a dictionary.
> 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.
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
Peter Larsen [CPH] - 01 Apr 2008 15:25 GMT 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
>> Is it possible to do something similar to the following: >> [quoted text clipped - 3 lines] > > It's not entirely clear to me what you actually want to do. Jon Skeet [C# MVP] - 01 Apr 2008 15:40 GMT > 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.
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
Paul E Collins - 01 Apr 2008 14:12 GMT > How do i concat two dictionaries aka the following sample: > Dictionary<int, string> a; > Dictionary<int, string> b; 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.
Cowboy (Gregory A. Beamer) - 01 Apr 2008 14:13 GMT 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.
 Signature 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, > [quoted text clipped - 8 lines] > BR > Peter Peter Larsen [CPH] - 01 Apr 2008 15:03 GMT 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
> Are their any potential clashes in the key? In other words, suppose the > following: [quoted text clipped - 18 lines] > > If you have clashes, you will have to loop. Cowboy (Gregory A. Beamer) - 03 Apr 2008 18:02 GMT 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.
 Signature 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! *************************************************
>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 :-) [quoted text clipped - 23 lines] >> >> If you have clashes, you will have to loop. Peter Larsen [CPH] - 04 Apr 2008 07:53 GMT Thanks for the links Gregory..
/Peter
> One of the best, human readible that is, explanations of Lambda > expressions is found in the book Introducing Microsoft(r) LINQ. You can [quoted text clipped - 4 lines] > Methods to Lambda Expressions. Once you follow the path, you should have a > decent idea of how to navigate this jungle. Jarlaxle - 01 Apr 2008 15:53 GMT 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
> Hi, > [quoted text clipped - 8 lines] > BR > Peter Jon Skeet [C# MVP] - 01 Apr 2008 15:59 GMT > write an extension method! > [quoted text clipped - 10 lines] > ... > 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);
?
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
Jarlaxle - 01 Apr 2008 16:41 GMT > 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.
> > write an extension method! > > [quoted text clipped - 18 lines] > > ? Jon Skeet [C# MVP] - 01 Apr 2008 16:47 GMT <snip>
> 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.
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
Peter Larsen [CPH] - 02 Apr 2008 09:48 GMT Thank you for you comments. I think my question has been answered by now.
/Peter
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|