Custom Aggregate error

5 posts, 1 answers
  1. Tommy Z
    Tommy Z avatar
    9 posts
    Member since:
    Sep 2009

    Posted 18 Jan 2012 Link to this post

    I am attempting to write a custom aggregator to sum TimeSpan values.  I kept getting the "No generic method" error so I scoured these forums and pulled down a couple of simple projects to play with.  But I'm getting the same error, "No generic method 'Sum' on type 'CustomAggregateFunctionSL.MyAggregates' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic."

    Here is a small snippet showing what I'm doing in the page.
    public partial class MainPage : UserControl
        {
             
            public MainPage()
            {
                 
                InitializeComponent();
                List<Item> items = new List<Item>();
                for (int i = 0; i < 10; i++)
                {
                    items.Add(new Item(){ Value1 = TimeSpan.FromSeconds(i), Type=(i%2==0?"P":"Q")});
                }
                this.RadGridView1.ItemsSource = items;
                this.RadGridView1.Columns[0].AggregateFunctions.Add(new MyAggregateFunction() { Caption = "Sum of Time's" });
            }
        }
     
        public class Item
        {
            public TimeSpan Value1 { get; set; }
            public String Type{ get; set; }
        }


    And here is the code for the custom aggregator.
    public class MyAggregateFunction : EnumerableAggregateFunction
        {
            protected override string AggregateMethodName
            {
                get
                {
                    return "Sum";
                }
            }
     
            protected override Type ExtensionMethodsType
            {
                get
                {
                    return typeof(MyAggregates);
                }
            }
        }
     
        public class MyAggregates
        {
            public static TimeSpan Sum<TSource>(IEnumerable<TSource> source, Func<TSource, TimeSpan> selector)
            {
                return source.Select(selector).Aggregate(TimeSpan.Zero, (t1, t2) => t1 + t2);
            }
        }


    What am I doing wrong?  I am using 2011.3.1116.1040.

  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 19 Jan 2012 Link to this post

    Hello Doug,

    Please take a look at this forum thread for a reference. 

    Kind regards,
    Maya
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. Tommy Z
    Tommy Z avatar
    9 posts
    Member since:
    Sep 2009

    Posted 19 Jan 2012 Link to this post

    Hi Maya,

    Thanks for the response.  I had actually already read that thread and used it to write the test project.  I figure the problem must be in how I'm defining the function.  I've tried both

    public static TimeSpan Sum<TSource>(IEnumerable<TSource> source, Func<TSource, TimeSpan> selector)

    and
    public static TimeSpan Sum<TSource, TTimeSpan>(IEnumerable<TSource> source, Func<TSource, TimeSpan> selector)

    with the same results. 
  4. Answer
    Maya
    Admin
    Maya avatar
    4062 posts

    Posted 20 Jan 2012 Link to this post

    Hi Doug,

    What you can try is use a generic AggregateExpression instead of defining a custom function. It could be something as follows:

    var aggregateFunction = new AggregateFunction<Player, TimeSpan>
                {
                    AggregationExpression = myItem => myItem.Aggregate(TimeSpan.Zero, (subtotal, t) => subtotal.Add(t.MyProperty))
                };
                this.playersGrid.Columns[4].AggregateFunctions.Add(aggregateFunction);

    In this case 'MyProperty' will be the one from your business object of type TimeSpan.

    Regards,
    Maya
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  5. Tommy Z
    Tommy Z avatar
    9 posts
    Member since:
    Sep 2009

    Posted 20 Jan 2012 Link to this post

    Many thanks Maya, that helped lead me in the right direction!
Back to Top