AsUnordered
在處理大量元素時,有序序列可能會損害效能。為了緩解這種情況,可以在不再需要序列順序時呼叫 AsUnordered
。
var sequence = Enumerable.Range(1, 10000).Select(x => -1 * x); // -1, -2, ...
var evenNumbers = sequence.AsParallel()
.OrderBy(x => x)
.Take(5000)
.AsUnordered()
.Where(x => x % 2 == 0) // This line won't be affected by ordering
.ToList();