仅合并两个词典的键的最佳方法是什么?
例如我们有:
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"}};
我想收到的是包含{“1”,“2”,“3”}的列表。
答案 0 :(得分:3)
只需合并到Key
数组:
var a = _dictionary1.Keys.Union(_dictionary2.Keys);
答案 1 :(得分:1)
使用Enumerable.Union()方法的唯一键:
var mergedKeys = _dictionary1.Keys.Union(_dictionary2.Keys);
使用Enumerable.Concat()方法的所有密钥:
var mergedKeys = _dictionary1.Keys.Concat(_dictionary2.Keys);
答案 2 :(得分:1)