Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I have a collection of Model classes and I wan't to Map each class individually to View Model classes.

I can use Parallel.ForEach or ParallelEnumerable.ForAll, so what is the difference between the 2, if any?

I couldn't find anything on MSDN.

share|improve this question

marked as duplicate by svick, George Duckett, Pete, nawfal, Albireo Jun 7 '13 at 8:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

add comment

1 Answer

up vote 4 down vote accepted

I can use Parallel.ForEach or ParallelEnumerable.ForAll, so what is the difference between the 2, if any?

Parallel.ForEach is the equivelent to using foreach in normal code.

The ParallelEnumerable.ForAll method is effectively the equivelent to List<T>.ForEach.

Both will perform and work similarly, though the ForAll method requires you to be using PLINQ already, as you need a ParallelEnumerable. Parallel.ForEach works directly off any IEnumerable<T>.

In general, I tend to prefer Parallel.ForEach, as the sole purpose of ForAll is to cause side effects. There are some disadvantages to this, as described in detail by Eric Lippert.

share|improve this answer
    
uh huh, so basically they are pretty much identical except for the fact that ParallelEnumerable requires an actual ParallelEnumerable. –  Stan R. Jun 6 '13 at 23:58
    
I agree with the side-effect part, but I want to map items in parallel in this case I think it is valid to do so. Correct me if I am wrong, but Parallel.ForEach does indeed execute iterations in parallel...right? –  Stan R. Jun 7 '13 at 0:00
    
@StanR. Yes, it does. "Mapping" suggests a PLINQ Select() clause, not a ForAll() statement, though, as Select is effectively a mapping operation... –  Reed Copsey Jun 7 '13 at 0:01
2  
I think I am wasting time here, since i would have to collect all my mapped items back into a concurrent list probably not worth it for a small list in terms of context switching. –  Stan R. Jun 7 '13 at 0:05
    
thank you Reed :) –  Stan R. Jun 7 '13 at 0:08
show 2 more comments

Not the answer you're looking for? Browse other questions tagged or ask your own question.