合并两个字典的键

本文关键字:字典 两个 合并 | 更新日期: 2023-09-27 18:09:34

只合并两个字典的键的最佳方法是什么?

例如:

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"}的列表

合并两个字典的键

仅结合Key数组:

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

使用Enumerable.Union()方法的唯一键:

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

使用Enumerable.Concat()方法的所有键:

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

ConcatUnion方法