I want to remove all items that have Tag set to string Dynamic:
foreach (MenuItem item in Items.Cast<object>().Where(mi => mi is MenuItem && ((MenuItem) mi).Tag == "Dynamic"))
{
Items.Remove(item);
}
Unfortunately, it won’t allow me to remove items while iterating the collection.
Is there a simple way to remove all items that have a certain condition met true, in this case Tag set to a string Dynamic?
As you cannot change a collection while iterating it, you will have to store the result items and remove it in a second step.