I have a large collection of elements. I want to call ToString for each element and build one string. My first approach was to slow
string str = "";
list.ForEach( g => {
string s = g.ToString();
if(s != "")
str = str + g.ToString() + "\n";
});
I tried using the Parallel class and PLINQ as shown below but then the order of the elements in the final string was not like in the original.
Parallel
System.Threading.Tasks.Parallel.ForEach(list, g => {
string s = g.ToString();
if(s != "")
str = str + g.ToString() + "\n";
});
PLINQ
string str = "";
list.AsParallel().AsOrdered().ForAll( g => {
string s = g.ToString();
if(s != "")
str = str + g.ToString() + "\n";
});
How can I improve the performance and keep the original order? Thanks