4thSpace
4thSpace

Reputation: 44352

How to remove items in IEnumerable<MyClass>?

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

RemoveAll() does not apply.

Upvotes: 3

Views: 27916

Answers (9)

T. Kiley
T. Kiley

Reputation: 2802

As everyone has already stated, you can't remove from IEnumerable because that is not what the interface is describing.

Consider the following example:

public IEnumerable<string> GetSomeStrings()
{
    yield return "FirstString";
    yield return "Another string";
}

Clearly, removing an element from this IEnumerable is not something you can reasonably do, instead you'd have to make a new enumeration without the ones you don't want.

The yield keyword provides other examples, for example, you can have infinite lists:

public IEnumerable<int> GetPowersOf2()
{
    int value = 1;

    while(true)
    {
        yield return value;
        value = value * 2;
    }
}

Upvotes: 1

Vishal Kottarathil
Vishal Kottarathil

Reputation: 346

This is how i do,

IEnumerable<T> myVar=getSomeData(); // Assume mayVar holds some data

myVar=myVar.Where(d=>d.Id>10); // thats all, i want data with Id>10 only

Upvotes: 0

Coding Tumbleweed
Coding Tumbleweed

Reputation: 134

How about trying Enumerable.Empty i.e.

T obj = new T();
IEnumerable<T> myVar = new T[]{obj}    //Now myVar has an element
myVar = Enumerable.Empty<T>();        //Now myVar is empty

Upvotes: -1

Bassam Alugili
Bassam Alugili

Reputation: 17043

You can cast it and use the List<T>.RemoveAll(Predicate<T> match) this is exactly what you need.

Upvotes: 0

Dan Puzey
Dan Puzey

Reputation: 34218

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.

Upvotes: 12

Servy
Servy

Reputation: 203838

You don't remove items from an IEnumerable. It's not possible. It's just a sequence of items. You can remove items from some underlying source that generates the sequences, for example if the IEnumerable is based on a list you can remove items from that list.

The other option you have is to create a new sequence, based on this one, that never shows the given items. You can do that using Where, but it's important to realize this isn't removing items, but rather choosing to show items based on a certain condition.

Upvotes: 1

Tim S.
Tim S.

Reputation: 56586

You can't remove items from an IEnumerable<T>. You can remove items from an ICollection<T> or filter items from an IEnumerable<T>.

// filtering example; does not modify oldEnumerable itself
var filteredEnumerable = oldEnumerable.Where(...);
// removing example
var coll = (ICollection<MyClass>)oldEnumerable;
coll.Remove(item);

Upvotes: 1

Lasse Christiansen
Lasse Christiansen

Reputation: 10325

Items cannot be removed from an IEnumerable<T>. From the documentation:

Exposes the enumerator, which supports a simple iteration over a collection of a specified type.

Upvotes: 0

evanmcdonnal
evanmcdonnal

Reputation: 48154

This will produce a new collection rather than modifying the existing one however I think it is the idiomatic way to do it with LINQ.

    var filtered = myCollection.Where(x => x.SomeProp != SomValue);

Another option would be to use Where to produce a new IEnumerable<T> with references to the objects you want removed then pass that to a Remove call on the original collection. Of course that would actually consume more resources.

Upvotes: 3

Related Questions