4

我有一个 3 元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);

制作清单

Item1 | Item2 | Item3
---------------------
"bar" | "foo" | "baz"
---------------------
"fun" | "bun" | "pun"
4

2 回答 2

16

看看RemoveAll方法List<T>。_ 它允许您根据谓词删除项目。

例如,您可以只检查Item2属性,正如您在问题中所说:

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

请注意(正如方法名称所暗示的那样),这将删除与此条件匹配的所有元素。因此,如果有多个元素的Item2属性"dog"为 ,则它们都将被删除。

于 2012-12-16T14:46:50.073 回答
5
myTupleList.RemoveAll( p => p.Item2 == "dog");
于 2012-12-16T14:47:42.150 回答