Collection was modified; enumeration operation may not execute

后端 未结 16 2558
南旧
南旧 2020-11-21 06:05

I can\'t get to the bottom of this error, because when the debugger is attached, it does not seem to occur.

Collection was modified; enumeration operatio

16条回答
  •  孤独总比滥情好
    2020-11-21 07:04

    I had the same issue, and it was solved when I used a for loop instead of foreach.

    // foreach (var item in itemsToBeLast)
    for (int i = 0; i < itemsToBeLast.Count; i++)
    {
        var matchingItem = itemsToBeLast.FirstOrDefault(item => item.Detach);
    
       if (matchingItem != null)
       {
          itemsToBeLast.Remove(matchingItem);
          continue;
       }
       allItems.Add(itemsToBeLast[i]);// (attachDetachItem);
    }
    

提交回复
热议问题