将多个词典合并为一个词典

可能重复:
合并C#中的词典

字典1

“a”,“1”
“b”,“2”

字典2

“c”,“3”
“d”,“4”

字典3

“e”,“5”
“f”,“6”

组合字典

“a”,“1”
“b”,“2”
“c”,“3”
“d”,“4”
“e”,“5”
“f”,“6”

如何将上述3个词典合并为一个组合词典?

var d1 = new Dictionary(); var d2 = new Dictionary(); var d3 = new Dictionary(); var result = d1.Union(d2).Union(d3).ToDictionary (k => k.Key, v => v.Value); 

编辑
确保没有重复密钥使用:

 var result = d1.Concat(d2).Concat(d3).GroupBy(d => d.Key) .ToDictionary (d => d.Key, d => d.First().Value); 

只需循环它们:

 var result = new Dictionary(); foreach (var dict in dictionariesToCombine) { foreach (var item in dict) { result.Add(item.Key, item.Value); } } 

(假设dictionariesToCombine IEnumerable是你的词典中的一些IEnumerable来组合,比如一个数组。)