在C#字典的关键字中搜索部分单词并更新其布尔值

在C#字典的关键字中搜索部分单词并更新其布尔值,c#,.net,winforms,C#,.net,Winforms,我以以下方式声明了一本词典: public static Dictionary<string,bool> Features {get;set;} 填充之后,我需要迭代字典键并更新字典值 例:无论我在键中有.add和.delete,它的值都需要更新为false Features.Keys.Where(v => v.EndsWith(".add") || v.EndsWith(".delete")) .ToList().ForEach(v => Features[v

我以以下方式声明了一本词典:

public static Dictionary<string,bool> Features {get;set;}
填充之后,我需要迭代字典键并更新字典值

例:无论我在键中有.add和.delete,它的值都需要更新为false

Features.Keys.Where(v => v.EndsWith(".add") || v.EndsWith(".delete"))
    .ToList().ForEach(v => Features[v] = false);
更新后,它必须是这样的

module1.add,false
module1.update,true
module1.delete,false
module1.save,true
module1.clear,true
module2.add,false
module2.update,true
module2.delete,false
module2.save,true
...
module10.add,false
module10.update,true
module10.delete,false
module10.save,true
让我知道如何遍历字典并更新C#中的值。
非常感谢您的帮助。

这是您更新特定词典中的值的方式

foreach (var item in Features) {
    if (item.Key.Contains("delete") || item.Key.Contains("add"))
        Features[item.Key] = false;
}
bool newValue = true;
if (myDictionary.ContainsKey("module10.add")) {
    myDictionary["module10.add"] = newValue;
}
foreach (string key in myDictionary.Keys) {
    myDictionary[key] = newValue;
}
这是您循环字典中所有项目的方式

bool newValue = true;
if (myDictionary.ContainsKey("module10.add")) {
    myDictionary["module10.add"] = newValue;
}
foreach (string key in myDictionary.Keys) {
    myDictionary[key] = newValue;
}

这就是如何更新特定词典中的值

bool newValue = true;
if (myDictionary.ContainsKey("module10.add")) {
    myDictionary["module10.add"] = newValue;
}
foreach (string key in myDictionary.Keys) {
    myDictionary[key] = newValue;
}
这是您循环字典中所有项目的方式

bool newValue = true;
if (myDictionary.ContainsKey("module10.add")) {
    myDictionary["module10.add"] = newValue;
}
foreach (string key in myDictionary.Keys) {
    myDictionary[key] = newValue;
}

这将获取以.add或.delete结尾的所有键,并将其值更新为false

Features.Keys.Where(v => v.EndsWith(".add") || v.EndsWith(".delete"))
    .ToList().ForEach(v => Features[v] = false);

这里没有linq:

var targetFeatuers = new List<string>();
foreach (var feature in Features.Keys)
{
    if (feature.EndsWith(".add") || feature.EndsWith(".delete"))
    {
        targetFeatuers.Add(feature);
    }
}

foreach (var feature in targetFeatuers)
{
    Features[feature] = false;
}
var targetfeatures=new List();
foreach(Features.Keys中的var特性)
{
if(feature.EndsWith(“.add”)| | feature.EndsWith(“.delete”))
{
targetFeatures.Add(特征);
}
}
foreach(TargetFeatures中的var功能)
{
特征[特征]=假;
}

请注意,您必须创建单独的键列表,以避免在枚举集合时修改集合。

这将获取以.add或.delete结尾的所有键,并将其值更新为false

Features.Keys.Where(v => v.EndsWith(".add") || v.EndsWith(".delete"))
    .ToList().ForEach(v => Features[v] = false);

这里没有linq:

var targetFeatuers = new List<string>();
foreach (var feature in Features.Keys)
{
    if (feature.EndsWith(".add") || feature.EndsWith(".delete"))
    {
        targetFeatuers.Add(feature);
    }
}

foreach (var feature in targetFeatuers)
{
    Features[feature] = false;
}
var targetfeatures=new List();
foreach(Features.Keys中的var特性)
{
if(feature.EndsWith(“.add”)| | feature.EndsWith(“.delete”))
{
targetFeatures.Add(特征);
}
}
foreach(TargetFeatures中的var功能)
{
特征[特征]=假;
}


请注意,您必须创建单独的键列表,以避免在枚举集合时对其进行修改。

我认为找到答案要比发布问题快得多。您的键显然是由名词(模块)和动词(添加/删除/等)组合而成的,因此,难怪您在实现方面遇到了一些困难。总的来说,我认为最好将模块和动词拆分成单独的字典,这样你就有了功能[“module1”][“add”]等等。我想找到答案要比发布问题快得多。你的键很明显是合成的——你有一个名词(模块)和动词(add/delete/etc),因此,难怪您在实现方面遇到了一些困难。总的来说,我认为最好将模块和动词拆分成单独的词典,这样你就有了功能[“module1”][“add”]等等。这样会不会很慢,并且否定了词典的用途?@neolik可能,是的。你有更好的解决方案吗?建议重新设计。原因很明显,此处词典未按预期用途使用。此外-
集合已修改;枚举操作可能无法执行
原因是在foreach@Neolisk你可能是对的,但这不能再回答OP的问题了。它会不会很慢,这样就否定了字典的用途?@neolik可能,是的。你有更好的解决方案吗?建议重新设计。原因很明显,此处词典未按预期用途使用。此外-
集合已修改;枚举操作可能无法执行
原因是在foreach@Neolisk你可能是对的,但这不能再回答OP的问题了。我无法使用。在哪里,我使用的是VS2008。这就是问题所在吗?@VikasKunte你有没有
使用System.Linq在你的班级中名列前茅?@Jon,你知道为什么这种方法不会导致
集合被修改;当丹尼斯的答案出现错误时,枚举操作可能不会执行
错误?@Manatherin:谢谢。错误发生了。测试上面的一段代码code@jon:非常感谢。我无法使用它。在哪里,我使用的是VS 2008。这就是问题所在吗?@VikasKunte你有没有
使用System.Linq在你的班级中名列前茅?@Jon,你知道为什么这种方法不会导致
集合被修改;当丹尼斯的答案出现错误时,枚举操作可能不会执行
错误?@Manatherin:谢谢。错误发生了。测试上面的一段代码code@jon:非常感谢。它起作用了