Removing Items in a List While Iterating Through It with For Each Loop

前端 未结 7 2095
小鲜肉
小鲜肉 2021-01-17 09:56

I have a list named NeededList I need to check each item in this list to see if it exists in my database. If it does exist in the database I need to remove it

7条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-17 10:16

    No you can not remove from a List that you are working on e.g. For Each Str As String In listOfStrings If Str.Equals("Pat") Then Dim index = listOfStrings.IndexOf(Str) listOfStrings .RemoveAt(index) End If Next

    But this way will work make a copy of your list and delete from it e.g. For Each Str As String In listOfStrings If Str.Equals("Pat") Then Dim index = listOfStringsCopy.IndexOf(Str) listOfStringsCopy.RemoveAt(index) End If Next

提交回复
热议问题