C#合并字典2 [英] C# Merging 2 dictionaries

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

问题描述

我在开发C#.NET针对3.5的应用程序。在这里面,我有一个包含验证标准为一组特定的我的应用程序的元素2类似的字典。这两个字典具有相同的签名。第一个词典有默认的设置和第二字典包含一些用户自定义设置。

  VAR default_settings =新词典<字符串,MyElementSettings>(); 
变种custom_settings =新词典<字符串,MyElementSettings>();



我想在2字典组合成一个既包含字典的元素。



这是我遇到的问题是,它是可能的字典都具有一些相同的键值。基本的规则我要的是同时拥有字典的组合,如果在已经在default_settings存在任何custom_settings按键,custom_settings值将覆盖default_settings值。最好的解决办法,我有只是一个foreach循环,检查重点在其他的字典存在,如果没有,添加它。

 的foreach(在custom_settings VAR项)
{
如果(default_settings.ContainsKey(item.Key))
default_settings [item.Key] = item.Value;
,否则
default_settings.Add(item.Key,item.Value);
}



我已经做了一些基本的LINQ查询,但我仍然努力学习更先进的东西。我已经看到了一些疑问,这将合并2词典,但是大多数涉及分组的任何元素有重复键,或只刚刚重复键返回集合/是否有一个LINQ查询或表达式会模仿foreach循环的行为我使用


解决方案

两点:




  1. LINQ是不太适合执行的副作用。在这种情况下,你试图变异现有的集合,而不是执行查询,所以我会从一个纯粹的LINQ解决方案回避

  2. 的的setter上通用词典的索引已经添加键值对,如果键不存在,或者覆盖的效果如果它的价值。




当您设置属性值,如果
中的关键是在词典中,用
该键相关联的值是由所分配的
值为替换。如果键不在
字典,键和
值为被添加到词典中。




所以,你的的foreach 循环基本上等同于:

 的foreach(VAR项目在custom_settings)
{
default_settings [item.Key] = item.Value;
}

现在那是相当的简洁了,所以我不认为LINQ是要帮助你所有的东西。


I'm developing an app in C# targeting .NET 3.5. In it, I have 2 similar dictionaries that contain validation criteria for a specific set of elements in my app. Both dictionaries have identical signatures. The first dictionary has the default settings and the 2nd dictionary contains some user defined settings.

var default_settings = new Dictionary<string, MyElementSettings>();
var custom_settings = new Dictionary<string, MyElementSettings>();

I would like to combine the 2 dictionaries into one that contains the elements of both dictionaries.

The problem that I am running into is it is possible for both dictionaries to have the some of the same key values. The basic rule I want is to have a combination of both dictionary and if there are any keys in the custom_settings that already exist in the default_settings, the custom_settings value will overwrite the default_settings value. The best solution i have is just a foreach loop, check if the key exists in the other dictionary, and if not, add it.

foreach (var item in custom_settings)
{
    if (default_settings.ContainsKey(item.Key))
        default_settings[item.Key] = item.Value;
    else
        default_settings.Add(item.Key, item.Value);
}

I've done some basic LINQ queries, but I'm still working on learning the more advanced stuff. I've seen a few queries that will merge 2 dictionaries, but most involve grouping any element with duplicate keys, or only return a collection with just the duplicate keys/ Is there a LINQ query or expression that will mimic the behavior of the foreach loop I am using?

解决方案

Two points:

  1. LINQ isn't great for executing side effects. In this case, you're attempting to mutate an existing collection rather than execute a query, so I would shy away from a pure LINQ solution.
  2. The setter on the generic dictionary's indexer already has the effect of adding the key-value pair if the key doesn't exist, or overwriting the value if it does.

When you set the property value, if the key is in the Dictionary, the value associated with that key is replaced by the assigned value. If the key is not in the Dictionary, the key and value are added to the dictionary.

So your foreach loop is essentially equivalent to:

foreach (var item in custom_settings)
{
   default_settings[item.Key] = item.Value;
}

Now that's pretty terse already, so I don't think LINQ is going to help you all that much.

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

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