This reminds me a little bit of MapReduce. You have a collection of objects you want to map (e.g. select a single property of the object) to a numeric value and after that you want to reduce this new collection of numeric values to a single numeric value. You could define a Wrapper-Method (for easier use):

    public static TResult MapReduce<T, TMap, TResult>(this IEnumerable<T> source, Func<T, TMap> mapper, Func<IEnumerable<TMap>, TResult> reducer)
        return reducer(source.Select(mapper));

Define Operation-Method as follows:

    public void Operation(IEnumerable<TestClass> samples, Func<IEnumerable<double>, double> operation)
        var foo = samples.MapReduce(s => s.Foo, operation);
        var bar = samples.MapReduce(s => s.Bar, operation);

And call it with:

        this.Operation(new List<TestClass>(), Enumerable.Average);
        this.Operation(new List<TestClass>(), MyLinqExtensions.StandardDeviation);

But you have to implement your StandardDeviation a little bit different:

    internal static double StandardDeviation(this IEnumerable<double> source)
        var average = source.Average();
        var sumOfSquares = source.Sum(sample => Math.Pow(sample - average, 2));
        return Math.Pow(sumOfSquares, 0.5);

Related Articles