简体   繁体   中英

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

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"

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM