连接两个字典 [英] Concatenate two Dictionaries

查看:84
本文介绍了连接两个字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提供一些字典

Dictionary<string, string> GroupNames = new Dictionary<string, string>();
Dictionary<string, string> AddedGroupNames = new Dictionary<string, string>();

我无法将它们合并为一个:

I am unable to merge them into one:

GroupNames = GroupNames.Concat(AddedGroupNames);

因为无法隐式转换类型".我相信(并且我的代码证明我是对的)它们的类型相同-我忽略了什么?

because "the type can't be implicitly converted". I believe (and my code proves me true) their type is the same - what am I overlooking?

推荐答案

我认为您已将GroupNames定义为Dictionary<string,string>,因此您需要像这样添加ToDictionary:

I think you defined your GroupNames as Dictionary<string,string>, so you need to add ToDictionary like this:

GroupNames = GroupNames.Concat(AddedGroupNames)
                       .ToDictionary(x=>x.Key,x=>x.Value);

请注意,两个原始词典将具有不同的键,否则我们需要一些规则来正确合并它们.

Note that 2 original dictionaries would have different keys, otherwise we need some rule to merge them correctly.

这篇关于连接两个字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆