I’d like to write a Map-Extension Method for ParallelQuery without destroying the parallelism. Problem is, I have no idea how. I am using ParallelQuery because I’m confident the Multithreading will boost my performance, here’s my code so far:
public static List<T2> Map<T, T2>(this ParallelQuery<T> source, Func<T, T2> func)
{
List<T2> result = new List<T2>();
foreach(T item in source)
{
result.Add(func(item));
}
return result;
}
As you can see, this kind of defeats the purpose of parallelism if I am correct. How would I do this correctly?
Thanks to Dykam for pointing out the the Select-Method has exactly the behaviour I want. However, just for learning purposes I’d like to see how exactly this’d work, thanks!
Your question piqued my developer’s heart, so I picked it up again and came up with this one:
The
ForAllloops through thesourcequery in parallel and adds the result to a thread-safe collection.I keep this one in mind when I need Select-like behaviour with some personal twist. It may be an efficient approach when function
funcis a relatively heavy process.