DataTable 上的并行 ForEach [英] Parallel ForEach on DataTable

查看:25
本文介绍了DataTable 上的并行 ForEach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用新的 Parallel.ForEach 函数来遍历数据表并对每一行执行操作.我正在尝试转换以下代码:

I would like to use the new Parallel.ForEach function to loop through a datatable and perform actions on each row. I am trying to convert the code below:

        foreach(DataRow drow in dt.Rows)
        {
           ...
           Do Stuff
           ...
        }

此代码:

        System.Threading.Tasks.Parallel.ForEach(dt.Rows, drow =>
                {
                    ...
                    Do Stuff
                    ...
                });

当我运行新代码时出现错误:

When I run the new code I get the error:

无法从用法推断方法System.Threading.Tasks.Parallel.ForEach(System.Collections.Generic.IEnumerable, System.Action)"的类型参数.尝试明确指定类型参数.

正确的语法是什么?

推荐答案

DataTable.Rows 返回一个 DataRowCollection,它只实现了 IEnumerable,而不是IEnumerable.使用 AsEnumerable()<DataTable 上的/a> 扩展方法(来自 DataTableExtensions):

DataTable.Rows returns a DataRowCollection which only implements IEnumerable, not IEnumerable<DataRow>. Use the AsEnumerable() extension method on DataTable (from DataTableExtensions) instead:

Parallel.ForEach(dt.AsEnumerable(), drow =>
{
    ...
    Do Stuff
    ...
});

这篇关于DataTable 上的并行 ForEach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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