From the previous question I asked, RemoveAll is the cleanest way to remove from a List<> based on a condition. Curious to know what is the best way to remove from a LinkedList as there is no RemoveAll function there.
List<ItemClass> itemsToErase = new List<ItemClass>();
foreach(ItemClass itm in DS)
{
if(itm.ToBeRemoved)
itemsToErase .Add(itm);
}
foreach(ItemClass eraseItem in itemsToErase)
{
DS.Remove(eraseItem );
}
EDIT: DS is of type LinkedList<ItemClass>
While you can’t remove nodes from a LinkedList<T> while iterating it with
foreach, you can manually iterate the LinkedList<T> by following the Next property of each LinkedListNode<T>. Just remember the next node of the node before removing it:Extension Method:
Usage:
See also: Extension Methods (C# Programming Guide)