从 VB.NET 列表中删除特定值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33153655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove Specific Value From List In VB.NET
提问by Ryan Thomas
I have a list of values, these are all a combination of letters and numbers, to make an ID, however on some occasions this can just simply be a 0.
我有一个值列表,这些都是字母和数字的组合,用于制作 ID,但在某些情况下,这可能只是一个 0。
I need to remove all occasions where this is a 0.
我需要删除所有为 0 的场合。
I've tried something like
我试过类似的东西
For i = 0 the list.count - 1
If list(i) = "0" Then
list.RemoveAt(j)
End If
Next
But this then throws an argument out of range exception at the end because the loop continues, and I can't use Exit For because I need to check for multiple zeros.
但是这会在最后抛出一个参数超出范围异常,因为循环继续,我不能使用 Exit For 因为我需要检查多个零。
I'm not very good at the Lamda expressions that seem to do what I want, and don't understand them, so if someone could give and explain one that would work, that'd be brilliant.
我不太擅长 Lamda 表达式,它们似乎可以做我想做的事,但不理解它们,所以如果有人能给出并解释一个可行的表达式,那就太棒了。
回答by Mike_OBrien
using LINQ or lambda expressions would be fairly easy solution to this problem.
使用 LINQ 或 lambda 表达式可以很容易地解决这个问题。
The IEnumerable(Of T) interface contains a method called RemoveAll. This function can seem a bit daunting because it takes in a rather nasty looking parameter but it really isn't as bad as it looks.
IEnumerable(Of T) 接口包含一个名为 的方法RemoveAll。这个函数看起来有点令人生畏,因为它接受了一个看起来很讨厌的参数,但它实际上并没有看起来那么糟糕。
So the parameter passed into RemoveAllcan be an anonymous function or an address to a function. For this case an anonymous function is the way to go.
所以传入的参数RemoveAll可以是匿名函数,也可以是函数的地址。对于这种情况,匿名函数是要走的路。
To remove all the objects from a list of strings where a condition is met you can use:
要从满足条件的字符串列表中删除所有对象,您可以使用:
list.RemoveAll(Function(o) o="0")
This short line of code is actually doing quite a few things. First its creating an anonymous function that takes in a string as a parameter and returns the result of the comparison of that string to the literal "0". Next the RemoveAllfunction uses that anonymous function to evaluate all the members of the listand removes the object if the anonymous function evaluates to true.
这段简短的代码实际上做了很多事情。首先它创建一个匿名函数,该函数接受一个字符串作为参数,并返回该字符串与文字“0”的比较结果。接下来,该RemoveAll函数使用该匿名函数来计算 的所有成员,list如果匿名函数的计算结果为真,则删除该对象。
Alternative:
选择:
The same code could be done with a function pointer. This method results in a bit more code but allows you to do a lot more in your comparison logic than just compare each value to a literal.
可以使用函数指针来完成相同的代码。这种方法会产生更多的代码,但允许您在比较逻辑中做更多的事情,而不仅仅是将每个值与文字进行比较。
We still call the RemoveAllfunction like before but this time we pass the address of a function that has the signature similar to Public Function <FunctionName>(byval Param As String) As Booleanif the signature does not match then you will get an error.
我们仍然RemoveAll像以前一样调用该函数,但这次我们传递了一个具有类似签名的函数的地址,Public Function <FunctionName>(byval Param As String) As Boolean如果签名不匹配,则您将收到错误消息。
List.RemoveAll(AddressOf TestFunction)
Next we define the function off somewhere else in the program.
接下来我们在程序的其他地方定义函数。
Public Function TestFunction(ByVal item As String) As Boolean
return item = "0"
End Function
回答by Ceres
Start from the end of the list and go backwards toward 0
从列表的末尾开始向后向 0
For i = list.count - 1 to 0 Step -1
If list(i) = "0" Then
list.RemoveAt(j)
End If
Next
If you want to use linq with a lambda use Where to keep all items that are not equal to "0" and recreate the list with ToList
如果您想将 linq 与 lambda 一起使用,请使用 Where 保留所有不等于“0”的项目并使用以下命令重新创建列表 ToList
list = list.Where(function(value) value <> "0").ToList
The lamda in this case is the same as
这种情况下的 lamda 与
Function IsNotZero(value As String) As Boolean
return value <> "0"
End Function
回答by Bradley Uffner
The trick is to loop backwards
诀窍是向后循环
For i = thelist.count - 1 to 0 step -1
Because you are going backwards you don't have to worry about shifting the items down as you go and messing up your index.
因为您正在倒退,所以您不必担心在前进时将项目向下移动并弄乱索引。
回答by Fabio
Another approach can be creating new list which doesn't contain "0"-values
另一种方法是创建不包含“0”值的新列表
Dim newlist As List(Of String) = list.Where(function(v) v.Equals("0") = false).ToList()

