linq orderby.tolist() performance
I have an ordering query to a List and calling for many times.
list = list.OrderBy().ToList();
In this code T开发者_如何学编程oList() 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?
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();
You only need to call ToList() once to get your sorted list. All future actions should use sortedList.
sortedList = list.OrderBy().ToList();
精彩评论