Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove object from generic list by id

I have a domain class like this:

public class DomainClass
{
  public virtual string name{get;set;}
  public virtual IList<Note> Notes{get;set;}
}

How would I go about removing an item from the IList<Note>? I would be able to do it if it was a List but it has to be an IList as I am using Nhibernate for my persistance layer.

Ideally I wanted a method like this in my domain class:

public virtual void RemoveNote(int id)
{
   //remove the note from the list here

   List<Note> notes = (List<Note>)Notes

   notes.RemoveAll(delegate (Note note)
   {
       return (note.Id = id)
   });
}

But I can't cast the IList as a List. Is there a more elegant way round this?

like image 539
gdp Avatar asked Jun 25 '11 13:06

gdp


People also ask

How do I remove an instance from a List?

The remove() method will remove the first instance of a value in a list. The pop() method removes an element at a given index, and will also return the removed item. You can also use the del keyword in Python to remove an element or slice from a list.

How to delete an object from a list in Java?

For Deleting Object from a list, Remove method must be used. Remove Method deletes the value to be deleted. If the value to be deleted is more than one in the list, it removes the first value. This method is often used to remove values with reference types. But it can also be used with value types. Following example we creates a student class.

How to remove duplicates from a list<T> generic class?

Several properties and methods of the List<T> generic class are used to add, insert, and search the list. After these operations, the list contains a duplicate. The Remove method is used to remove the first instance of the duplicate item, and the contents are displayed. The Remove method always removes the first instance it encounters.

What is the return value of remove (Object OBJ) method of list?

Return Value: It returns a boolean value True after removing the first occurrence of the specified element from the List and otherwise if the element is not present in the List then this method will return False. Below program illustrate the remove (Object obj) method of List in Java:

How to remove a single item from a list of IDS?

If ID is not unique use IDictionary<int, IList<Note>> instead. Show activity on this post. You can either code it manually. The naive implementation is O (n*k) with n the number of items in the list, and k the number of items you want to remove. If you want to just remove a single item it is fast.


2 Answers

You could filter out the items you don't want and create a new list with only the items you do want:

public virtual void RemoveNote(int id)
{
   //remove the note from the list here

   Notes = Notes.Where(note => note.Id != id).ToList();
}
like image 167
Gabe Avatar answered Sep 19 '22 05:09

Gabe


Edit2: This method doesn't require casting to a List!

foreach (var n in Notes.Where(note => note.Id == id).ToArray()) Notes.Remove(n);

or...

Notes.Remove(Notes.Where(note => note.Id == id).First());

The first one is the best.
The second one will throw an exception if no notes have that id.

Edit: Thanks to Magnus and rsbarro for showing my mistake.

like image 20
Vercas Avatar answered Sep 18 '22 05:09

Vercas