4thSpace 4thSpace - 2 months ago 7
C# Question

How to remove items in IEnumerable<MyClass>?

How do I remove items from a IEnumerable that match specific criteria?

RemoveAll() does not apply.

Answer

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.