C# 从字典中删除特定条目(在列表中定义)<;字符串>;<;字符串,int>;给出严重错误-->;
我有一个字符串列表集合。这样,C# 从字典中删除特定条目(在列表中定义)<;字符串>;<;字符串,int>;给出严重错误-->;,c#,list,dictionary,generics,collections,C#,List,Dictionary,Generics,Collections,我有一个字符串列表集合。这样, public List<string> filterWords = new List<string>() { "an" , "be", " ", "in", "the", "
public List<string> filterWords = new List<string>()
{
"an" ,
"be",
" ",
"in",
"the",
"of"
..
...
.
.
....
};
目的是检查/查找并从上述词典中删除包含上一列表中的键的条目。等一下
到目前为止,我编写的基本代码是--
已使用idx=0,因为我只需要字典值中的默认值0。逻辑似乎还可以。这是一个非常精确的解决方案。任何关键的lambda表达式都能满足需要吗?“wordsToRemove”上的foreach有什么问题
注意我张贴的代码块。有一行注释dictionary=wordsToRemove.Select………
我试着在那一个和foreach循环dictionary.Add中交替注释。这两种情况下的错误是相同的
**仔细阅读添加到代码块的注释
Thx.如果您想删除,让我们
删除
:
List<string> filterWords = ...
// I have a dictionary collection in format of Dictionary<string, int>, Like so
Dictionary<string, int> dictionary = ...
...
// then we Remove each word in wordsToRemove from dictionary
foreach (string word in filterWords)
dictionary.Remove(word);
List filterWords=。。。
//我有一本字典集,是字典的形式,就像这样
字典=。。。
...
//然后我们从字典中删除wordsToRemove中的每个单词
foreach(过滤词中的字符串字)
删除(单词);
如果要删除,让我们删除
:
List<string> filterWords = ...
// I have a dictionary collection in format of Dictionary<string, int>, Like so
Dictionary<string, int> dictionary = ...
...
// then we Remove each word in wordsToRemove from dictionary
foreach (string word in filterWords)
dictionary.Remove(word);
List filterWords=。。。
//我有一本字典集,是字典的形式,就像这样
字典=。。。
...
//然后我们从字典中删除wordsToRemove中的每个单词
foreach(过滤词中的字符串字)
删除(单词);
hi@DimitryBychenko——略有变化。我发布的最初代码有误。Pl现在检查过滤词列表。仅包含单词,没有我最初发布的0。错误为-->长度不能小于零。参数名称:长度。Pl参考列表filterwords code block.hi@DimitryBychenko中的更改--轻微更改。我发布的最初代码有误。Pl现在检查过滤词列表。仅包含单词,没有我最初发布的0。错误为-->长度不能小于零。参数名称:长度。Pl参考列表过滤器字代码块中的更改。
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value) at ....
List<string> filterWords = ...
// I have a dictionary collection in format of Dictionary<string, int>, Like so
Dictionary<string, int> dictionary = ...
...
// then we Remove each word in wordsToRemove from dictionary
foreach (string word in filterWords)
dictionary.Remove(word);