0

I was making a custom grid that accepts an IEnumerable as an Itemsource. However I was not able to remove an Item inside the itemsource during delete method. Will you guys be able to help me using the code below?

static void Main(string[] args)
{
    List<MyData> source = new List<MyData>();
    int itemsCount = 20;
    for (int i = 0; i < itemsCount; i++)
    {
       source.Add(new MyData() { Data = "mydata" + i });
    }

    IEnumerable mItemsource = source;
    //Remove Sample of an mItemSource
    //goes here ..
}

public class MyData { public string Data { get; set; } }
Omar
  • 16,329
  • 10
  • 48
  • 66
plutomusang
  • 59
  • 1
  • 6
  • You probably don't even need `mItemsource` since you are wanting to use the original list functionality of `source`. Just remove that and work with your local `source` variable. – David Anderson Aug 08 '12 at 02:38
  • I think you should also read this - what is the difference between ienumberable, array, IList and List http://stackoverflow.com/questions/764748/whats-the-difference-between-ienumerable-and-array-ilist-and-list?lq=1 – Clinton Ward Aug 08 '12 at 02:50
  • Your code is not clearly showing what you are trying to do. You should show how you have some object that accepts your `IEnumerable`, so that it is clear that you *only have* an `IEnumerable`, and that the `List` is just for example data reference. – Jonathon Reinhart Aug 08 '12 at 15:46

3 Answers3

7

You can't. IEnumerable (and its generic counterpart IEnumerable<T>) is for just that - enumerating over the contents of some collection. It provides no facilities for modifying the collection.

If you are looking for an interface that provides all the typical means of modifying a collection (eg. Add, Remove) then have a look at ICollection<T> or IList<T> if you need to access elements by index.

Or, if your goal is to provide an IEnumerable to something, but with some items removed, consider Enumerable.Except() to filter them out (as it is enumerated).

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • ...which means you can probably do `.ToList().Remove(whateverItem)` on your IEnumerable. – Andrew Jan 22 '14 at 20:28
  • 1
    @Andrew That is a horribly inefficient way to accomplish this. Not to mention that it's not actually modifying the original `source` list, but rather a copy. – Jonathon Reinhart Jan 22 '14 at 23:21
  • That's a good point. I don't guess you would ever need to do that, really. – Andrew Jan 23 '14 at 04:09
  • 1
    Remember, any "decent" programmer can string [BCL](http://en.wikipedia.org/wiki/Base_Class_Library) calls together to make things "work". A "good" programmer understands what the library is doing under the hood, and takes advantage of the proper data structures, algorithms, etc. – Jonathon Reinhart Jan 23 '14 at 04:12
-2

Use while loop to traverse the list whilst delete.

int i = 0;
while(i < source.Count){
    if(canBeRemoved(source[i])){
        source.RemoveAt(i);
    }else{
        i++;    
    }
}
Shao Jun
  • 31
  • 4
-2

I was able to remove Item from the Itemsource using dynamic

    static void Main(string[] args)
    {

        List<MyData> source = new List<MyData>();
        int itemsCount = 20;
        for (int i = 0; i < itemsCount; i++)
        {
            source.Add(new MyData() { Data = "mydata" + i });
        }

        IEnumerable mItemsource = source;

        //Remove Sample of an mItemSource

        dynamic d = mItemsource;
        d.RemoveAt(0);

        //check data
        string s = source[0].Data;
    }
    public class MyData { public string Data { get; set; } }
Bryan
  • 1
  • 3
    -1 This only works because **you know** that the `IEnumerable` is pointing to a `List`. What happens if someone passes an array instead? It will blow up. – Jonathon Reinhart Aug 08 '12 at 15:45