Concat two Dictionary<> 合并Dictionary<>

by Admin9. July 2010 11:12

 Question

 

How do i concat two dictionaries aka the following sample:
Dictionary<int, string> a;
Dictionary<int, string> b;
b.Add(a);

Answer

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);

Tags:

.Net

Add comment

  Country flag

biuquote
Loading

我的饭否

Widget RandomPosts not found.

The file '/blog/widgets/RandomPosts/widget.ascx' does not exist.X

RecentComments

Comment RSS

Google PageRank