How do I remove an element that matches a given criteria from a LinkedList in C#?
-
02-07-2019 - |
Question
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.
Solution
list.Remove(list.First(e => e.id == searchId));
OTHER TIPS
Here's a simple solution:
list.Remove(list.First((node) => node.id == searchId));
Just use the Where extension method. You will get a new list (IIRC).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow