Priscila
Priscila

Reputation: 301

Delete an element from a generic list

I can´t remove an element from an IEnumerable list, but this list is a reference to a List , a private attribute of an other class. If I put personsCollection.Remove(theElement) in the same class (class Manager), it works perfect, but I need to delete the element since the other class (class ManagerDelete). Please how can I do this? Thanks.

class Other
{
  //Some code
  public IEnumerable<Person> SearchByPhone (string value)
    {
        return from person in personCollection
               where person.SPhone == value
               select person;
    }
 }

class ManagerDelete
{ 
//Some code
IEnumerable<Person> auxList= SearchByPhone (value);
//I have a method for delete here  
}

class Manager
{ 
//Some code
 private List<Person> personsCollection = new List<Person>();
}

Upvotes: 7

Views: 589

Answers (7)

Vlad
Vlad

Reputation: 35594

In addition to other answers: your data structure choice doesn't seem to be adequate. If you really plan to remove elements from a container, your container must not be an IEnumerable<> in the first place!

Consider the following choices:

  • switch from IEnumerable<> to ICollection<>;
  • replace removing with filtering out (.Where()) and obtaining a separate filtered IEnumerable<>.

Upvotes: 1

Darren
Darren

Reputation: 70728

You can't delete from an IEnumerable<T> you need to make it of type IList<T> to add/remove items directly from it.

Upvotes: 12

juharr
juharr

Reputation: 32276

You can use ToList to convert the IEnumerable to a List and then you can remove stuff from it.

var auxList= SearchByPhone (value).ToList();
auxList.Remove(something);

Upvotes: 2

Derek
Derek

Reputation: 8628

You need to understand what an IEnumerable Interface allows you to do.

I'v listed below the list of Interfaces that you should use by design, as and when required in differnt circumstances. In your example IList is what you will need to use.

  • ICollection Defines general characteristics (e.g., size, enumeration, and thread safety) for all non-generic collection types.

  • ICloneable Allows the implementing object to return a copy of itself to the caller.

  • IDictionary Allows a non-generic collection object to represent its
    contents using key/value pairs.

  • IEnumerable Returns an object implementing the IEnumerator interface (see next table entry).

  • IEnumerator Enables foreach style iteration of collection items.

  • IList Provides behavior to add, remove, and index items in a
    sequential list of objects.

Upvotes: 4

user1064248
user1064248

Reputation:

Darren is right. Without converting to a list you can do:

personCollection = personCollection.Except(SearchByPhone(value));

in ManagerDelete.

Upvotes: 3

Arran
Arran

Reputation: 25056

An IEnumerable is literally just an interface to say, I've got this collection, you can iterate over it.

If you take a look at the actual details of the IEnumerable interface, it doesn't contain much, only a method to allow callers to iterate over it:

http://msdn.microsoft.com/en-gb/library/9eekhta0.aspx

The reason why List types can allow you to remove them, is it's built on top of the IEnumerable interface, giving you more functionality. It is supposed to represent a collection of objects you can manipulate. Whereas an IEnumerable is simply not designed to do this.

The Remove method actually stems from the underlying ICollection interface that List also implements:

http://msdn.microsoft.com/en-gb/library/bye7h94w.aspx

Upvotes: 1

Marek Dzikiewicz
Marek Dzikiewicz

Reputation: 2884

You can cast the IEnumerable to an IList. This will work if the IEnumerable is really a List. However this is a bad design. If you want to remove items, you should expose the list as an IList, not IEnumerable.

Upvotes: 1

Related Questions