我有一个三元组 List<Tuple>
与 string, string, string
.
初始化:List<Tuple<string, string string>> myTupleList = new List<Tuple<string, string, string>>();
我基本上想在 Item2
中搜索一个值并删除整个条目(如果找到)。
让我想象一下。如果我有:
Item1 | Item2 | Item3
---------------------
"bar" | "foo" | "baz"
---------------------
"cat" | "dog" | "sun"
---------------------
"fun" | "bun" | "pun"
我想做
//pseudocode
myTupleList.Remove("dog" in Item2);
制作 list
Item1 | Item2 | Item3
---------------------
"bar" | "foo" | "baz"
---------------------
"fun" | "bun" | "pun"
最佳答案
看看 RemoveAll
method的 List<T>
.它允许您根据谓词删除项目。
例如,您可以只检查 Item2
属性(property),正如您在问题中所说:
myTupleList.RemoveAll(item => item.Item2 == "dog");
请注意(正如方法名称所暗示的那样)这将删除与此条件匹配的所有元素。因此,如果有几个元素
Item2
属性等于 "dog"
,所有这些都将被删除。
关于C# - 如何从 List<Tuple> 中删除值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13902377/