I have a foreach
statement like
// distinctValues is DataTable with thousand of rows
foreach (DataRow row in distinctValues.Rows)
{
// Lots of Statements
}
Is there a way to execute forfach
Statement in Parallel for faster execution?
Well, the only (little) difficulty is that DataTable
(if I guessed distinctValues
type right) doesn't implement IEnumerable<DataRow>
interface However, you can easily obtain it with a help of .AsEnumerable()
extension method:
Parallel.ForEach
implemenation
Parallel.ForEach(distinctValues.AsEnumerable(), row => {
// Lots of Statements
});
PLinq (Parallel Linq) code which can be more convenient for experimenting:
distinctValues
.AsEnumerable()
.AsParallel()
//.AsOrdered()
//.WithMergeOptions(ParallelMergeOptions.NotBuffered)
//.WithDegreeOfParallelism(4)
.ForAll(row => {
// Lots of Statements
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments