views:

946

answers:

4

I have a LinkedList, where Entry has a member called id. I want to remove the Entry from the list where id matches a search value. What's the best way to do this? I don't want to use Remove(), because Entry.Equals will compare other members, and I only want to match on id. I'm hoping to do something kind of like this:

entries.RemoveWhereTrue(e => e.id == searchId);

edit: Can someone re-open this question for me? It's NOT a duplicate - the question it's supposed to be a duplicate of is about the List class. List.RemoveAll won't work - that's part of the List class.

A: 

Here's an answer to a duplicate question
http://stackoverflow.com/questions/16460/removing-n-items-from-a-list-conditionally

Slavo
It's not an exact duplicate, as this question refers to LinkedList whereas the question you linked refers to List.
Matt Howells
+1  A: 

Just use the Where extension method. You will get a new list (IIRC).

leppie
+3  A: 
list.Remove(list.First(e => e.id == searchId));
Matt Howells
See my answer - the RemoveAll method actually accepts a predicate as a parameter.
Slavo
Except there is no RemoveAll method on a LinkedList<T>.
muloh
This question is about a LinkedList, which doesn't have a RemoveAll method.
Matt Howells
"although it does unfortunately require two iterations through the list rather than one as in my solution"Nope. Remove(LinkedListNode<T>) is O(1)
munificent
+2  A: 

Here's a simple solution:

list.Remove(list.First((node) => node.id == searchId));
munificent
Gets my vote, since I can't vote for myself :)
Matt Howells