using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telerik.Windows.Data;
using System.Collections.ObjectModel;

namespace _778009_RadGridView_WPF_AR_137
{
    public class MyCustomFunction: AggregateFunction<Club, double>
    {
        public MyCustomFunction()
        {
            this.AggregationExpression = items => StdDev(items);
        }

        private double StdDev(IEnumerable<Club> source)
        {
            var itemCount = source.Count();
            if (itemCount > 1)
            {
                var values = source.Select(i => i.StadiumCapacity);
                var average = values.Average();
                var sum = values.Sum(v => Math.Pow(v - average, 2));
                return Math.Sqrt(sum / (itemCount - 1));
            }

            return 0;
        }
    }
}
