public static object Sum(ScriptThread thread, AstNode[] childNodes) { double sum = 0; var fieldName = childNodes[0].Evaluate(thread).ToString(); var dataSource = thread.GetDataSource(); var grouped = ImportAggregateHelper.IsGrouped(dataSource); if (grouped != null) { foreach (var element in grouped) { var s = element.Sum(o => { var v = ReadValueFromObject(fieldName, o); return TypeNormalizer.EnsureType<double>(v); }); sum = sum + s; } } else { if (ImportAggregateHelper.FieldExist(dataSource.FirstOrDefault(),fieldName)) { sum = dataSource.Sum(o => { var v = ReadValueFromObject(fieldName, o); return TypeNormalizer.EnsureType<double>(v); }); } } return sum.ToString(); }
public static object Sum(ScriptThread thread, AstNode[] childNodes) { double sum = 0; object firstItem = null; bool isTimeSpan; var fieldName = childNodes[0].Evaluate(thread).ToString(); var dataSource = thread.GetDataSource(); var grouped = ImportAggregateHelper.IsGrouped(dataSource); if (grouped != null) { var firstGroupElement = dataSource.FirstOrDefault() as IGrouping<object, object>; firstItem = firstGroupElement.FirstOrDefault(); isTimeSpan = HandleTimeSpan(firstItem,fieldName); double groupSum = 0; foreach (var element in grouped) { if (isTimeSpan) { groupSum = element.Sum(o => {return TimeSpanSum(fieldName,o);}); } else { groupSum = element.Sum(o => {return SimpleSum(fieldName,o);}); } sum = sum + groupSum; } } else { firstItem = dataSource.FirstOrDefault(); isTimeSpan = HandleTimeSpan(firstItem,fieldName); if (ImportAggregateHelper.FieldExist(dataSource.FirstOrDefault(),fieldName)) { if (isTimeSpan) { sum = dataSource.Sum(o => {return TimeSpanSum(fieldName,o); }); } else { sum = dataSource.Sum(o => {return SimpleSum(fieldName,o); }); } } } return sum.ToString(); }