4thSpace 4thSpace - 1 year ago 83
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download