在列表中的Foreach循环中删除键值对

时间:2021-12-20 19:28:10

I want to remove a key-value pair from List after displaying it But it gives me error message like [ Collection was modified; enumeration operation may not execute. exception]

我想在显示它之后从List中删除一个键值对但是它给了我一个错误消息,如[Collection was modified;枚举操作可能无法执行。例外]

Here is code

这是代码

if (senderRemainingSeqList.Count > 0)
{
      senderRemainingSeqList.Sort();

      foreach (var row in senderRemainingSeqList)
      {
          if ( row.Key == currentSenderID)
          {
               if (row.Value == senderNextSeqList[currentSenderID])
               {
                 Console.WriteLine("{0} {1}",row.Key,row.Value);
                  senderRemainingSeqList.Remove(row);
               }
         }
}}

4 个解决方案

#1


You can't modify the collection you are enumerating on, instead you can filter out values not matching your criteria and the create a new list using ToList and assign it back to your senderRemainingSeqList:

您无法修改您枚举的集合,而是可以过滤掉与您的条件不匹配的值,并使用ToList创建新列表并将其分配回senderRemainingSeqList:

senderRemainingSeqList = senderRemainingSeqList
                .Where(row=> row.Key != currentSenderID && 
                 row.Value != senderNextSeqList[currentSenderID])
                .ToList();

#2


You can loop on copy of the initial list, and so you're allowed to modify it:

您可以循环访问初始列表的副本,因此您可以修改它:

  ...
  // Note ToList(); 
  foreach (var row in senderRemainingSeqList.ToList()) {
    ...
  }
  ...

#3


use a for loop if you want to modify the collection or create a RemoveList during enumeration and use that.

如果要在枚举期间修改集合或创建RemoveList并使用它,请使用for循环。

#4


Try doing a reverse loop to avoid mistakes while removing.

尝试进行反向循环以避免在删除时出错。

for(int i = list.Count -1; i >= 0; --i)
{ 
    // Determine if this is the pair you want to remove
    if(list[i].key == keyToRemove)
         list.RemoveAt(i);
}

You have to do this backwards otherwise the list might change structure and entries might be skipped.

您必须向后执行此操作,否则列表可能会更改结构,并且可能会跳过条目。

#1


You can't modify the collection you are enumerating on, instead you can filter out values not matching your criteria and the create a new list using ToList and assign it back to your senderRemainingSeqList:

您无法修改您枚举的集合,而是可以过滤掉与您的条件不匹配的值,并使用ToList创建新列表并将其分配回senderRemainingSeqList:

senderRemainingSeqList = senderRemainingSeqList
                .Where(row=> row.Key != currentSenderID && 
                 row.Value != senderNextSeqList[currentSenderID])
                .ToList();

#2


You can loop on copy of the initial list, and so you're allowed to modify it:

您可以循环访问初始列表的副本,因此您可以修改它:

  ...
  // Note ToList(); 
  foreach (var row in senderRemainingSeqList.ToList()) {
    ...
  }
  ...

#3


use a for loop if you want to modify the collection or create a RemoveList during enumeration and use that.

如果要在枚举期间修改集合或创建RemoveList并使用它,请使用for循环。

#4


Try doing a reverse loop to avoid mistakes while removing.

尝试进行反向循环以避免在删除时出错。

for(int i = list.Count -1; i >= 0; --i)
{ 
    // Determine if this is the pair you want to remove
    if(list[i].key == keyToRemove)
         list.RemoveAt(i);
}

You have to do this backwards otherwise the list might change structure and entries might be skipped.

您必须向后执行此操作,否则列表可能会更改结构,并且可能会跳过条目。