System.Data.Common.TimeSpanStorage.Aggregate C# (CSharp) Method

Aggregate() public method

public Aggregate ( int records, AggregateType kind ) : object
records int
kind AggregateType
return object
        public override object Aggregate(int[] records, AggregateType kind)
        {
            bool hasData = false;
            try
            {
                switch (kind)
                {
                    case AggregateType.Min:
                        TimeSpan min = TimeSpan.MaxValue;
                        for (int i = 0; i < records.Length; i++)
                        {
                            int record = records[i];
                            if (IsNull(record))
                                continue;
                            min = (TimeSpan.Compare(_values[record], min) < 0) ? _values[record] : min;
                            hasData = true;
                        }
                        if (hasData)
                        {
                            return min;
                        }
                        return _nullValue;

                    case AggregateType.Max:
                        TimeSpan max = TimeSpan.MinValue;
                        for (int i = 0; i < records.Length; i++)
                        {
                            int record = records[i];
                            if (IsNull(record))
                                continue;
                            max = (TimeSpan.Compare(_values[record], max) >= 0) ? _values[record] : max;
                            hasData = true;
                        }
                        if (hasData)
                        {
                            return max;
                        }
                        return _nullValue;

                    case AggregateType.First:
                        if (records.Length > 0)
                        {
                            return _values[records[0]];
                        }
                        return null;

                    case AggregateType.Count:
                        return base.Aggregate(records, kind);

                    case AggregateType.Sum:
                        {
                            decimal sum = 0;
                            foreach (int record in records)
                            {
                                if (IsNull(record))
                                    continue;
                                sum += _values[record].Ticks;
                                hasData = true;
                            }
                            if (hasData)
                            {
                                return TimeSpan.FromTicks((long)Math.Round(sum));
                            }
                            return null;
                        }

                    case AggregateType.Mean:
                        {
                            decimal meanSum = 0;
                            int meanCount = 0;
                            foreach (int record in records)
                            {
                                if (IsNull(record))
                                    continue;
                                meanSum += _values[record].Ticks;
                                meanCount++;
                            }
                            if (meanCount > 0)
                            {
                                return TimeSpan.FromTicks((long)Math.Round(meanSum / meanCount));
                            }
                            return null;
                        }

                    case AggregateType.StDev:
                        {
                            int count = 0;
                            decimal meanSum = 0;

                            foreach (int record in records)
                            {
                                if (IsNull(record))
                                    continue;
                                meanSum += _values[record].Ticks;
                                count++;
                            }

                            if (count > 1)
                            {
                                double varSum = 0;
                                decimal mean = meanSum / count;
                                foreach (int record in records)
                                {
                                    if (IsNull(record))
                                        continue;
                                    double x = (double)(_values[record].Ticks - mean);
                                    varSum += x * x;
                                }
                                ulong stDev = (ulong)Math.Round(Math.Sqrt(varSum / (count - 1)));
                                if (stDev > long.MaxValue)
                                {
                                    stDev = long.MaxValue;
                                }
                                return TimeSpan.FromTicks((long)stDev);
                            }
                            return null;
                        }
                }
            }
            catch (OverflowException)
            {
                throw ExprException.Overflow(typeof(TimeSpan));
            }
            throw ExceptionBuilder.AggregateException(kind, _dataType);
        }