How to remove items in IEnumerable<MyClass>?

.net, c#, lambda

Solution

You can't; `IEnumerable` as an interface does not support removal.

If your `IEnumerable` instance is actually of a type that supports removal (such as `List<T>`) then you can cast to that type and use the `Remove` method.

Alternatively you can copy items to a different `IEnumerable` based on your criteria, or you can use a lazy-evaluated query (such as with Linq's `.Where`) to filter your `IEnumerable` on the fly. Neither of these will affect your original container, though.

Problem

How do I remove items from a IEnumerable that match specific criteria? RemoveAll() does not apply.

Original source

Related problems