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?
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 => ...);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With