I use linq to objects and I find out .ToList and .ToArray operation is blocking the thread. (In performance report, I see blocked time.) I guess it’s because GC is trying to allocate the memory when I call tolist or toarray. This is bad because I’m using linq in a parallel loop and I wish to access some shared collection in the linq.
Is there a way to solve this except not using linq to object?
I use linq to objects and I find out .ToList and .ToArray operation is
Share
Calling
ToListorToArraycause the query to get evaluated immediately – this will block the thread they are called on until the entire collection has been evaluated and the results of the query returned.You should iterate over the query directly without calling either method – this will stream the results in (assuming no other eager evaluation occurs).
I suggest you take a look at the Task Parallel Library (TPL) if you need to do concurrent work.