删除列表中的项目时,通过每个循环迭代 [英] Removing Items in a List While Iterating Through It with For Each Loop

查看:154
本文介绍了删除列表中的项目时,通过每个循环迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 NeededList 的列表,我需要检查这个列表中的每个项目,看它是否存在于我的数据库中。如果它存在于数据库中,我需要从列表中删除它。但是我在迭代时无法更改列表。





这是我的代码到目前为止: $ c $ For Each Needed In NeededList 
Dim Ticker = Needed.Split( - )(0).Trim()
Dim Year = Needed.Split( - )(1).Trim ()
Dim Period = Needed.Split( - )(2).Trim()
Dim Table = Needed.Split( - )(3).Trim()
Dim dr As OleDbDataReader
Dim cmd2 As New OleDb.OleDbCommand(SELECT * FROM& Table&WHERE Ticker =?AND [Year] =?AND Period =?,con)
cmd2 .Parameters.AddWithValue(?,Ticker)
cmd2.Parameters.AddWithValue(?,Year)
cmd2.Parameters.AddWithValue(?,句点)
dr = cmd2 .ExecuteReader
如果dr.HasRows Then
NeededList.Remove(需要)
结束如果
下一个


解决方案

不,您可以使用a for each,但是您可以使用旧式的for循环来完成此操作。

诀窍是从结尾开始,向后循环。通过这种方式,当您从列表中移除元素时,您将移向第一个元素,并且不会跳过元素,因为当前元素已被删除,并且您的索引器增加了数量。

<$对于x = NeededList.Count - 1到0步骤-1
Dim Needed = NeededList(x)
.....
如果dr。 HasRows Then
NeededList.RemoveAt(x)
End If
Next



<相反,如果循环到集合的末尾并决定删除元素,则跳过下一个元素。例如,假设您删除集合中的第四个元素,那么第五个元素将成为第四个元素。但是现在索引器上升到了5.这样,以前的第五个元素(现在在第四个位置)从来没有被评估过。当然你可以尝试改变索引器的值,但是这样做总是以糟糕的代码和错误等待发生。


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 from the list. But I can't change the list while I'm iterating through it. How can I make this work?

Here is my code so far:

For Each Needed In NeededList
        Dim Ticker = Needed.Split("-")(0).Trim()
        Dim Year = Needed.Split("-")(1).Trim()
        Dim Period = Needed.Split("-")(2).Trim()
        Dim Table = Needed.Split("-")(3).Trim()
        Dim dr As OleDbDataReader
        Dim cmd2 As New OleDb.OleDbCommand("SELECT * FROM " & Table & " WHERE Ticker = ? AND [Year] = ? AND Period = ?", con)
        cmd2.Parameters.AddWithValue("?", Ticker)
        cmd2.Parameters.AddWithValue("?", Year)
        cmd2.Parameters.AddWithValue("?", Period)
        dr = cmd2.ExecuteReader
        If dr.HasRows Then
            NeededList.Remove(Needed)
        End If
Next

解决方案

No you can't do that using a for each, but you can do that using the old fashioned for .. loop.
The trick is to start from the end and looping backwards. In this way, while you remove elements from the list, you move towards the first element and you never skip elements because the current one has been deleted and your indexer is incremented.

For x = NeededList.Count - 1 to 0 Step -1
    Dim Needed = NeededList(x)
    .....
    If dr.HasRows Then
        NeededList.RemoveAt(x)
    End If
Next

Instead, if you loop towards the end of the collection and decide to remove an element, you skip the next one. For example, suppose that you remove the fourth element in the collection, after that, the fifth element becomes the fourth. But now the indexer goes up to 5. In this way, the previous fifth element (now in fourth position) is never evaluated. Of course you could try to change the value of the indexer but this ends always in bad code and bugs waiting to happen.

这篇关于删除列表中的项目时,通过每个循环迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆