Parallel ForEach on DataTable
Solution 1:
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
...
});
Solution 2:
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.
Solution 3:
Parallel.ForEach() expects the first argument to be an IEnumerable<> type. DataTable.Rows is not, but you can turn it into one with the AsEnumerable() extension method. Try:
... Parallel.ForEach(dt.AsEnumerable(), drow => ...