Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get element index at PLinq ForAll

I have the following code:

line.Split(' ').AsParallel().ForAll(word =>
{
    // How to get element index?
}

How do I get the current element's index? Is it possible?

like image 934
Matias Cicero Avatar asked Oct 24 '25 04:10

Matias Cicero


2 Answers

There is an overload of Select that lets you access the index.

line.Split(' ')
    .AsParallel()
    .Select((w, i) => new { Index = i, Word = w })
    .ForAll(x => ...);
like image 59
Jakub Lortz Avatar answered Oct 27 '25 02:10

Jakub Lortz


Instead use Parallel.Foreach that can provide indexes too.

Parallel.ForEach(line.Split(' '),(word,state,index) =>
{

});

Note that this is not plinq. it requires to importSystem.Threading.Tasks

like image 30
M.kazem Akhgary Avatar answered Oct 27 '25 01:10

M.kazem Akhgary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!