简体   繁体   English

在Visual Basic中循环使用DataTable时出错

[英]Error looping through DataTable in Visual Basic

The code I have written below will do iteration of the loop properly. 我在下面编写的代码将正确迭代循环。 It tests the correct row against the condition, adds to the "secondary" DataTable and removes from the "master" as it is supposed to. 它根据条件测试正确的行,添加到“辅助”DataTable并按照预期从“master”中删除。 However, upon the second iteration of the loop I get the following error: 但是,在循环的第二次迭代中,我得到以下错误:

Collection was modified; enumeration operation might not execute.

Here is the code I am using 这是我正在使用的代码

            For Each row As DataRow In tblAgencyEdInfo.Rows
                Dim rDate As DateTime = row.Item("ExpirationDate")

                If rDate < DateTime.Now Then
                    tblExpEdInfo.ImportRow(row)
                    tblAgencyEdInfo.Rows.Remove(row)
                End If
            Next row

You cannot modify a collection during enumeration. 您无法在枚举期间修改集合。 That means you cannot add or remove items. 这意味着您无法添加或删除项目。 In this case you want to remove a DataRow from a DataTable in a For Each loop. 在这种情况下,您希望从For Each循环中的DataTable中删除DataRow。

The solution is either to 解决方案是要么

  1. use a For-Loop and iterate the items backwards, removing the rows via index or to 使用For-Loop并向后迭代项目,通过索引或删除行
  2. use another collection (fe a List(Of DataRow) ) and add the matching rows you want to remove in the For Each . 使用另一个集合( List(Of DataRow) )并在For Each添加要删除的匹配行。 Afterwards you just need to iterate this list and call tblAgencyEdInfo.Rows.Remove(rowToRemove) 之后你只需要迭代这个列表并调用tblAgencyEdInfo.Rows.Remove(rowToRemove)

For example: 例如:

Dim rowsToRemove = New List(Of DataRow)
For Each row As DataRow In tblAgencyEdInfo.Rows
    Dim rDate As DateTime = row.Item("ExpirationDate")
    If rDate < DateTime.Now Then
        rowsToRemove.Add(row)
    End If
Next row

For Each rowToRemove As DataRow In rowsToRemove 
    tblExpEdInfo.ImportRow(row)
    tblAgencyEdInfo.Rows.Remove(row)
Next rowToRemove 

The above is true, you cannot alter while using. 以上是真的,你在使用时不能改变。

        ' distinct
        Dim distinctTable As DataTable = tblProductsForDisplay.DefaultView.ToTable(True)
        ' get all bundles that contain products in bundles - GetBundleDataByOLLID
        For Each row As DataRow In distinctTable.Rows
            Dim myOLLCourseID As Integer = row.Item("OLLCourseID")
            tblProductsForDisplay.Merge(tblBundles(myOLLCourseID))
        Next

So what I did here was move datatable to another copy, using Distinct "ToTable(True)" to filter it even narrower/clean up. 所以我在这里做的是将数据移动到另一个副本,使用Distinct“ToTable(True)”来过滤它甚至更窄/清理。

Then use the original datatable to merge items, adding to the original, while looping through my copy. 然后使用原始数据表合并项目,添加到原始数据,同时循环我的副本。 My original now has all of the items plus the new ones. 我原来现在有所有项目和新项目。 Your logiv may alter original in different ways, but I just wanted to show by example. 您的logiv可能会以不同的方式改变原始,但我只想通过示例来展示。

RA RA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM