Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - How do I remove a value from a List<Tuple>

Tags:

c#

list

tuples

I have a 3-tuple List<Tuple> with string, string, string.
Initialization: List<Tuple<string, string string>> myTupleList = new List<Tuple<string, string, string>>();
I basically want to search for a value in Item2 and remove the whole entry if found.
Let me visualize. If I have:

Item1 | Item2 | Item3
---------------------
"bar" | "foo" | "baz"
---------------------
"cat" | "dog" | "sun"
---------------------
"fun" | "bun" | "pun"

I want doing

//pseudocode
myTupleList.Remove("dog" in Item2);

To make the List

Item1 | Item2 | Item3
---------------------
"bar" | "foo" | "baz"
---------------------
"fun" | "bun" | "pun"
like image 994
Стефан Дончев Avatar asked Nov 30 '22 13:11

Стефан Дончев


1 Answers

Have a look at the RemoveAll method of List<T>. It lets you remove an item based on a predicate.

For example, you can check just the Item2 property, as you said in your question:

myTupleList.RemoveAll(item => item.Item2 == "dog");

Note that (as implied by the method name) this will remove all elements that match this condition. Hence, if there are several elements whose Item2 property equals "dog", all of them will be removed.

like image 89
O. R. Mapper Avatar answered Dec 02 '22 03:12

O. R. Mapper