【发布时间】: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 行的列之一上设置检查状态?也许您可以改为执行数据库查询?