# Parallel For each Loop?

So how does this work?
Anyone have an example?
I’ve got two Ienumerable lists of elements that I need to traverse through and compare to one another… How could this potentially help… or would it not. Right now, I have a for each loop inside of another for each loop.

1 Like

You can use single for loop or two for loops (inner for loop) or LINQ queries.

Example1:

``````intCounter = 0
For Each item in List1
{
valuefromList1 = item
valuefromList2 = List2(intCounter)
intCounter = intCounter + 1
}
``````

Example2:

``````For Each item1 in List1
{
valuefromList1 = item1
For Each item2 in List2
{
valuefromList2 = item2
}
}
``````

You can use LINQ queries also for simple comparison.

``````    List matchingList = List1.Equals(List2).ToList
List firstNotSecond = List1.Except(List2).ToList
List secondNotFirst = List2.Except(List1).ToList
``````

Regards,
Karthik Byggari

2 Likes

Wouldn’t I also be able to do that in just a regular for each loop? How does the parallel part work?

1 Like

You mean like this?:

That would not work, because “item1” is only accessible within the scope of the first ForEach and “item2” is only accessible within the scope of the second ForEach, so you wouldn’t be able to compare both values. Plus, Parallel would not necessarily make it faster.

Normally, I would use LINQ to compare the value in a second enumerable, so I don’t have to use additional logic to loop through all items of the second enumerable.

But, you can use 2 ForEaches if you want, and compare the “item2” with “item1” within the second loop:

For LINQ, I would do something like this usually:

The condition will depend on what you are comparing.

Regards.

3 Likes

That’s all very interesting traversing the lists. I read every bit. But this is actually the activity that I’m talking about. How is this activity different than the regular for each activity? Do you put it in a parallel and then is it a special kind of for loop that does something magical that I don’t know about? Looks like it’s under System/Factories on the list of activities.

2 Likes

Oh I didn’t know about that one. After reading up on it, it can execute the iterations faster because they run in parallel. However, for many cases this would not help.
Here is a comparison that I looked at briefly: Parallel.ForEach() Vs Foreach() Loop in C#

I suppose this could speed up things like looking in a directory with many folders containing thousands of files. I have not used it so that’s just my understanding.

If you are wanting to execute each item with more visual tasks like interacting with applications, I think sticking to the standard For Each would be ideal.

4 Likes

Okay, so when it’s printing the fruit, it opens multiple threads, and prints them in parallel. That makes sense sort of. I can’t think of how I would implement that, but I’ll keep it in mind. Thank you.

2 Likes

I replicated the example you posted so I thought I would share. This actually could make some of my workflows twice as fast I guess.

2 Likes

SO I used the parallel for each loop on one of my larger loops that goes through adjustments, and rows and whatever… It came out to be the exact same time. My loop is very dependent on it’s activities, I don’t think there is a reasonable way to run multiple threads on it, because each activity in my loop is dependent on the previous activities. So There isn’t room for much improvement. Just a thought.

2 Likes

Never come across this activity.