开发者

Merging the keys of two dictionaries

开发者 https://www.devze.com 2023-04-07 20:36 出处:网络
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\",\"o

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

精彩评论

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