Custom aggregate functions throws System.ArgumentOutOfRangeException

4 posts, 1 answers
  1. Thomas LEBRUN
    Thomas LEBRUN avatar
    66 posts
    Member since:
    Sep 2009

    Posted 01 Dec 2009 Link to this post

    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 !
  2. Thomas LEBRUN
    Thomas LEBRUN avatar
    66 posts
    Member since:
    Sep 2009

    Posted 02 Dec 2009 Link to this post

    Hi,

    Anyone got an idea ?


    Thanks !
  3. DevCraft banner
  4. Answer
    Stefan Dobrev
    Admin
    Stefan Dobrev avatar
    790 posts

    Posted 02 Dec 2009 Link to this post

    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.
  5. Thomas LEBRUN
    Thomas LEBRUN avatar
    66 posts
    Member since:
    Sep 2009

    Posted 03 Dec 2009 Link to this post

    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 !
Back to Top