Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove an element that matches a given criteria from a LinkedList in C#?

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.

like image 694
TimK Avatar asked Sep 25 '08 13:09

TimK


3 Answers

list.Remove(list.First(e => e.id == searchId));
like image 155
Matt Howells Avatar answered Sep 28 '22 19:09

Matt Howells


Here's a simple solution:

list.Remove(list.First((node) => node.id == searchId));
like image 44
munificent Avatar answered Sep 28 '22 19:09

munificent


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

like image 28
leppie Avatar answered Sep 28 '22 20:09

leppie