linq orderby.tolist() performance

Baran picture Baran · Feb 14, 2011 · Viewed 13.5k times · Source

I have an ordering query to a List and calling for many times. list = list.OrderBy().ToList(); In this code ToList() method is spending high resources and takes very long time. How can I speed up with another ordering method without converting back to a list. Should I use .Sort extension for arrays?

Answer

CaptainPlanet picture CaptainPlanet · Feb 14, 2011

First of all, try to sort the list once, and keep it sorted.

To speed up things you can use Parallel LINQ.

see: http://msdn.microsoft.com/en-us/magazine/cc163329.aspx

An OrderBy() Parallel looks like this:

 var query = data.AsParallel().Where(x => p(x)).Orderby(x => k(x)).ToList();