This is a migrated thread and some comments may be shown as answers.

Custom aggregate functions throws System.ArgumentOutOfRangeException

3 Answers 175 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Thomas LEBRUN
Top achievements
Rank 1
Thomas LEBRUN asked on 01 Dec 2009, 10:23 AM
Hi,

I'm using the latest internal build with a custom aggregate function defined as belowed:

public abstract class IndexedAggregateFunction : AggregateFunction  
    {  
        static MethodInfo s_genericSelect = typeof(Enumerable).GetMethods().Where(m => m.Name == "Select" && m.GetParameters()[1].ParameterType.GetGenericArguments().Length == 2).First();  
 
        public override string FunctionName  
        {  
            get 
            {  
                return base.FunctionName;  
            }  
            set 
            {  
                base.FunctionName = value;  
            }  
        }  
        public override string ResultFormatString  
        {  
            get 
            {  
                return base.ResultFormatString;  
            }  
            set 
            {  
                base.ResultFormatString = value;  
            }  
        }  
 
        protected override string GenerateFunctionName()  
        {  
            return base.GenerateFunctionName();  
        }  
 
        public int Index { getset; }  
 
        protected abstract System.Linq.Expressions.Expression CreateAfterSelectionExpression(System.Linq.Expressions.Expression selectedEnumerableExpression);  
 
        protected System.Linq.Expressions.Expression GetDoubleSelectorExpression(System.Linq.Expressions.ParameterExpression param)  
        {  
            var propInfo = typeof(DataRowContainer).GetProperty("RawData");  
            var methodInfo = typeof(ObservableCollection<object>).GetMethod("get_Item");  
            return System.Linq.Expressions.Expression.Convert(  
                System.Linq.Expressions.Expression.Call(  
                    System.Linq.Expressions.Expression.MakeMemberAccess(param, propInfo)  
                    , methodInfo, System.Linq.Expressions.Expression.Constant(Index)),  
                typeof(double?));  
        }  
 
        protected System.Linq.Expressions.Expression GetListOfDoubleExpression(System.Linq.Expressions.Expression param)  
        {  
 
            var methodInfo = s_genericSelect.MakeGenericMethod(typeof(DataRowContainer), typeof(double?));  
 
            var selectorParam = System.Linq.Expressions.Expression.Parameter(typeof(DataRowContainer), "item");  
 
            return System.Linq.Expressions.Expression.Call(methodInfo, param, System.Linq.Expressions.Expression.Lambda(GetDoubleSelectorExpression(selectorParam), selectorParam));  
 
        }  
 
 
        protected System.Linq.Expressions.Expression GetAggregatedListOfDoubleExpression(System.Linq.Expressions.Expression param)  
        {  
            return CreateAfterSelectionExpression(GetListOfDoubleExpression(param));  
        }  
 
 
        public override System.Linq.Expressions.Expression CreateAggregateExpression(System.Linq.Expressions.Expression enumerableExpression)  
        {  
 
            return GetAggregatedListOfDoubleExpression(enumerableExpression);  
        }  
    }  
 
 
    public class IndexedSumAggregateFunction : IndexedAggregateFunction  
    {  
        protected override System.Linq.Expressions.Expression CreateAfterSelectionExpression(System.Linq.Expressions.Expression selectedEnumerableExpression)  
        {  
            var method = typeof(Enumerable).GetMethod("Sum"new Type[] { typeof(IEnumerable<double?>) });  
 
            return System.Linq.Expressions.Expression.Call(method, selectedEnumerableExpression);  
        }  
    } 


It works fine but sometimes, when i call CalculateAggregates, i get the following error:

System.ArgumentOutOfRangeException: Index was outsite the limit of the array

With the following stacktrace:

   à System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
   à System.ThrowHelper.ThrowArgumentOutOfRangeException()
   à System.Collections.Generic.List`1.get_Item(Int32 index)
   à System.Collections.ObjectModel.Collection`1.get_Item(Int32 index)
   à lambda_method(ExecutionScope , DataRowContainer )
   à System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   à System.Linq.Enumerable.Sum(IEnumerable`1 source)
   à lambda_method(ExecutionScope , IGrouping`2 )
   à System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   à Telerik.Windows.Data.QueryableExtensions.Aggregate(IQueryable source, IEnumerable`1 aggregateFunctions)
   à Telerik.Windows.Controls.GridView.GridViewDataControl.CreateAggregateResults()
   à Telerik.Windows.Controls.GridView.GridViewDataControl.CalculateAggregates()


Any ideas ?


Thanks !

3 Answers, 1 is accepted

Sort by
0
Thomas LEBRUN
Top achievements
Rank 1
answered on 02 Dec 2009, 08:33 AM
Hi,

Anyone got an idea ?


Thanks !
0
Accepted
Stefan Dobrev
Telerik team
answered on 03 Dec 2009, 03:19 AM
Hi Thomas,

It seems that the Index you are using in your aggregate expression is throwing the exception. For example one of your data items (DataRowContainer) the RawData property may has less number of elements than the index you are trying to access. This way an ArgumentOutOfRangeException will be thrown.

Greetings,
Stefan Dobrev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Thomas LEBRUN
Top achievements
Rank 1
answered on 03 Dec 2009, 08:07 AM
Hi Stefan,

I was using a foreach loop to add my custom aggregate function and in the loop, i called CalculateAggregate (not the best way to optimize performances :)

After move the call to CalculateAggregate outsite (after) the foreach loop, it seems to works fine.


Thanks !
Tags
GridView
Asked by
Thomas LEBRUN
Top achievements
Rank 1
Answers by
Thomas LEBRUN
Top achievements
Rank 1
Stefan Dobrev
Telerik team
Share this question
or