Here is my code:
foreach (OrderItem item in OrderInfo.order)
{
orderItemViews.Single(i => i.numericUpDown.Name == item.id.ToString()).numericUpDown.Value = item.count;
}
It gives an exception.
I know that I can't change the collection inside foreach
How can I change this code to make it work? Best of all if it would be LINQ code.
exception says that "The collection was modified". sorry can't provide real message of exception because it in non-english
sorry guys. I've found where collection is changing. It was inside *numericUpDown_ValueChanged* handler. anyway I've got an answer. thank you
This is what I do, when I need to modify the collection.
Create a copy. Enumerate the copy, but update the original one.
You can use an extension ToEach static method:
You can use
ToList()
, Like this :Or use normal for loop :
Tip : Performance wise, It's better to use the second way.