AlanTsai
1/3/2016 - 11:44 PM

From http://stackoverflow.com/questions/2714639/weighted-average-with-linq

public static double WeightedAverage<T>(this IEnumerable<T> records, Func<T, double> value, Func<T, double> weight)
    {
        double weightedValueSum = records.Sum(x => value(x) * weight(x));
        double weightSum = records.Sum(x => weight(x));

        if (weightSum != 0)
            return weightedValueSum / weightSum;
        else
            throw new DivideByZeroException("Your message here");
    }