【问题标题】:Parallel programming in VB.NETVB.NET 中的并行编程
【发布时间】:2014-03-26 18:13:10
【问题描述】:

我在按钮的点击中有一个简单的 For Each 循环,看起来像这样:

    Protected Sub SelectAllButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SelectAllButton.Click
    RadGrid1.AllowPaging = False
    RadGrid1.Rebind()
    For Each item In RadGrid1.Items
        TryCast(TryCast(item, GridDataItem)("template").FindControl("CheckBox1"), CheckBox).Checked = True
    Next
    SaveSelectedRFPs()
    RadGrid1.AllowPaging = True
    RadGrid1.Rebind()
End Sub

现在,对于每个循环,可能有超过 10 万条记录需要循环,我知道必须有一种更有效的方法来实现这一点,也许是并行编程?

@Nico Schetler:

我试过了,下面是代码的样子

    Protected Sub SelectAllButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SelectAllButton.Click
    RadGrid1.AllowPaging = False
    RadGrid1.Rebind()

    Parallel.ForEach(RadGrid1.Items.AsQueryable.Cast(Of GridDataItem).ToList(), Sub(item)
                                                                                    TryCast(TryCast(item, GridDataItem)("template").FindControl("CheckBox1"), CheckBox).Checked = True
                                                                                End Sub)
    SaveSelectedRFPs()
    RadGrid1.AllowPaging = True
    RadGrid1.Rebind()
End Sub

但是当我尝试运行它时,这给了我一个 Source is not IEnumerable 错误。

【问题讨论】:

  • 你试过Parallel.ForEach吗?
  • 你真的向用户展示了 100k 个项目吗?这对我来说似乎不是一个好主意。
  • 您应该使用底层业务对象,而不是直接使用您的网格,以便修改其中的数据。你想做什么?为什么需要在超过 100K 行的列之一上设置检查状态?也许您可以改为执行数据库查询?

标签: vb.net parallel.foreach


【解决方案1】:

如果我错了,请纠正我......(不,认真)

您不想选择数组中的每一项吗?

目前,据我所知,当您对所选集合执行操作时,您会检查哪个被选中。可能会在 SaveSelectedRFPs() 中添加一个参数,以确定是保存选定的 RFP 还是 未选定的 RFP。这可以为您节省整个循环过程。当然,您会失去视觉效果,但对于客户而言,这可能是值得的,因为它可以获得巨大的性能优势。

Public Sub SaveRFPs(SelectedOrUnselected As Boolean)
 If selectedRFP.Checked = SelectedOrUnselected Then
    selectedRFP.save()
 End If
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 2011-07-10
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多