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

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

问题描述

我想使用新的Parallel.ForEach函数循环访问数据表,并对每行执行操作。我试图转换下面的代码:

  foreach(DataRow drow in dt.Rows)
{$ b $ $ ...
>到此代码:

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

当我运行新代码时,我得到错误:



方法'System.Threading.Tasks.Parallel.ForEach(System.Collections.Generic.IEnumerable,System.Action)'的类型参数不能根据用法推断。尝试明确指定类型参数。



这是什么正确的语法?

解决方案

DataTable.Rows 返回一个 DataRowCollection IEnumerable ,而不是 IEnumerable< DataRow> 。使用 AsEnumerable() 扩展方法替换为 DataTable (来自 DataTableExtensions ):

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


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
           ...
        }

To this code:

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

When I run the new code I get the error:

The type arguments for method 'System.Threading.Tasks.Parallel.ForEach(System.Collections.Generic.IEnumerable, System.Action)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

What is the correct syntax for this?

解决方案

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天全站免登陆