Parallel ForEach on DataTable
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
...
});
This is better than the accepted answer because this does not need to reference System.Data.DataSetExtensions:
Parallel.ForEach(dt.Rows.Cast<DataRow>(), dr =>
To use ForEach with a non-generic collection, you can use the Cast extension method to convert the collection to a generic collection, as shown in this example.