Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

How to: Write a Custom PLINQ Aggregate Function

.NET Framework 4.6 and 4.5

This example shows how to use the Aggregate method to apply a custom aggregation function to a source sequence.

Caution note Caution

This example is intended to demonstrate usage, and might not run faster than the equivalent sequential LINQ to Objects query. For more information about speedup, see Understanding Speedup in PLINQ.

The following example calculates the standard deviation of a sequence of integers.

namespace PLINQAggregation
    using System;
    using System.Linq;

    class aggregation
        static void Main(string[] args)

            // Create a data source for demonstration purposes. 
            int[] source = new int[100000];
            Random rand = new Random();
            for (int x = 0; x < source.Length; x++)
                // Should result in a mean of approximately 15.0.
                source[x] = rand.Next(10, 20);

            // Standard deviation calculation requires that we first 
            // calculate the mean average. Average is a predefined 
            // aggregation operator, along with Max, Min and Count. 
            double mean = source.AsParallel().Average();

            // We use the overload that is unique to ParallelEnumerable. The  
            // third Func parameter combines the results from each thread. 
            double standardDev = source.AsParallel().Aggregate(
                // initialize subtotal. Use decimal point to tell  
                // the compiler this is a type double. Can also use: 0d.

                // do this on each thread
                 (subtotal, item) => subtotal + Math.Pow((item - mean), 2),

                 // aggregate results after all threads are done.
                 (total, thisThread) => total + thisThread,

                // perform standard deviation calc on the aggregated result.
                (finalSum) => Math.Sqrt((finalSum / (source.Length - 1)))
            Console.WriteLine("Mean value is = {0}", mean);
            Console.WriteLine("Standard deviation is {0}", standardDev);


This example uses an overload of the Aggregate standard query operator that is unique to PLINQ. This overload takes an extra System.Func<T1, T2, TResult> as the third input parameter. This delegate combines the results from all threads before it performs the final calculation on the aggregated results. In this example we add together the sums from all the threads.

Note that when a lambda expression body consists of a single expression, the return value of the System.Func<T, TResult> delegate is the value of the expression.

© 2015 Microsoft