none
Removing an item from an IEnumerable<String> list

    Question

  • How can you remove an item from a collection of data that is an IEnumerable<String> list? Do I need to convert this to a collection or something?

    Thanks!
    Wednesday, December 30, 2009 2:56 PM

Answers

  • I assumed that you are trying to remove an item from a method with a parameter of type IEnumerable<String> .  Since (I assume) the method now needs to remove an item, then IEnumerable<> is not enough.  Now the parameter type needs to be something else.

    A popular choice that is compatible with your current List<String> is IList<String> .  Always try to use interfaces instead of actual classes.  This way your methods work with other classes that implement the correct interfaces. :-)
    MCP
    • Marked as answer by eryang Wednesday, January 06, 2010 8:35 AM
    Wednesday, December 30, 2009 10:18 PM
  • oh, then did you try Remove() and RemoveAt() methods ?
    Thanks

    My BlogMy Facebook
    • Marked as answer by eryang Wednesday, January 06, 2010 8:35 AM
    Wednesday, December 30, 2009 3:09 PM
  • psychogeek:  IList<> inherits from IEnumerable<>.  You can use a foreach statement directly from the mylist object of yours.

    See http://msdn.microsoft.com/en-us/library/5y536ey6.aspx.
    MCP
    • Marked as answer by eryang Wednesday, January 06, 2010 8:35 AM
    Thursday, December 31, 2009 7:17 PM

All replies

  • Yes, you need a pointer to an interface other than IEnumerable<> because IEnumerable<> only provides a single method for traversing the collection.  Request a different pointer type.
    MCP
    Wednesday, December 30, 2009 3:02 PM
  • How do you request a pointer to a different type?
    Wednesday, December 30, 2009 3:04 PM
  • is it a custom Class ? or is it a list<string> ?

    edit:- hmm.. over to webJose..


    Thanks

    My BlogMy Facebook
    Wednesday, December 30, 2009 3:05 PM
  • It is just a list<string>, no custom class.
    Wednesday, December 30, 2009 3:08 PM
  • oh, then did you try Remove() and RemoveAt() methods ?
    Thanks

    My BlogMy Facebook
    • Marked as answer by eryang Wednesday, January 06, 2010 8:35 AM
    Wednesday, December 30, 2009 3:09 PM
  • Doh! I'll try that :-)
    Wednesday, December 30, 2009 3:14 PM
  • I assumed that you are trying to remove an item from a method with a parameter of type IEnumerable<String> .  Since (I assume) the method now needs to remove an item, then IEnumerable<> is not enough.  Now the parameter type needs to be something else.

    A popular choice that is compatible with your current List<String> is IList<String> .  Always try to use interfaces instead of actual classes.  This way your methods work with other classes that implement the correct interfaces. :-)
    MCP
    • Marked as answer by eryang Wednesday, January 06, 2010 8:35 AM
    Wednesday, December 30, 2009 10:18 PM
  • Not really sure how you're getting from List<string> to IEnumerable<string> (using a foreach?) but this code snippet may help:

                List<string> mylist = new List<string>(new string[] { "shane", "hello", "alien" });
                for (int i = mylist.Count - 1; i >= 0; i--)
                {
                    if (mylist[i] == "shane") mylist.RemoveAt(i);
                }

    The same concept works for many other collection types as well and it avoids IEnumerable<> altogether.

    HTH...but if not, Happy New Year anyway :)

    -Shane
    Thursday, December 31, 2009 11:38 AM
  • psychogeek:  IList<> inherits from IEnumerable<>.  You can use a foreach statement directly from the mylist object of yours.

    See http://msdn.microsoft.com/en-us/library/5y536ey6.aspx.
    MCP
    • Marked as answer by eryang Wednesday, January 06, 2010 8:35 AM
    Thursday, December 31, 2009 7:17 PM