开发者

Merging the keys of two dictionaries

What is the best way to merge only the keys of two dictionaries?

For example we have:

private readonly IDictionary<string, string> _dictionary1= new Dictionary<string, string>{{"1","one"},{"2","two开发者_开发百科"}};
private readonly IDictionary<string, string> _dictionary2 = new Dictionary<string, string>(){{"3","three"}};

What I want to receive is the list containing {"1","2","3"}.


Just combine to Key arrays:

var a = _dictionary1.Keys.Union(_dictionary2.Keys);


UNIQUE keys using Enumerable.Union() method:

  var mergedKeys = _dictionary1.Keys.Union(_dictionary2.Keys);

ALL keys using Enumerable.Concat() method:

var mergedKeys = _dictionary1.Keys.Concat(_dictionary2.Keys);


Look at Concat and Union methods.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜