相关疑难解决方法(0)

从ac#Dictionary中删除与谓词匹配的多个项目的最佳方法?

我需要从Dictionary中删除多个项目.一种简单的方法如下:

  List<string> keystoremove= new List<string>();
  foreach (KeyValuePair<string,object> k in MyCollection)
     if (k.Value.Member==foo)
        keystoremove.Add(k.Key);
  foreach (string s in keystoremove)
        MyCollection.Remove(s);
Run Code Online (Sandbox Code Playgroud)

我无法直接删除foreach块中的项目的原因是这会抛出异常("Collection was modified ...")

我想做以下事情:

 MyCollection.RemoveAll(x =>x.Member==foo)
Run Code Online (Sandbox Code Playgroud)

但是Dictionary <>类没有公开RemoveAll(Predicate <> Match)方法,就像List <> Class那样.

这样做的最佳方式(性能明智和优雅明智)是什么?

.net c# linq collections dictionary

51
推荐指数
5
解决办法
3万
查看次数

在枚举时修改.NET字典

我正在使用一个字典,我想在我枚举它时改变它,但似乎这是不允许的.我怎样才能做到这一点?

谢谢

.net c#

14
推荐指数
3
解决办法
6295
查看次数

标签 统计

.net ×2

c# ×2

collections ×1

dictionary ×1

linq ×1