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

Custom Aggregate error

4 Answers 30 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Tommy Z
Top achievements
Rank 1
Tommy Z asked on 18 Jan 2012, 10:40 PM
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.

4 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 19 Jan 2012, 07:37 AM
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 >>

0
Tommy Z
Top achievements
Rank 1
answered on 19 Jan 2012, 03:34 PM
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. 
0
Accepted
Maya
Telerik team
answered on 20 Jan 2012, 08:31 AM
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 >>

0
Tommy Z
Top achievements
Rank 1
answered on 20 Jan 2012, 10:38 PM
Many thanks Maya, that helped lead me in the right direction!
Tags
GridView
Asked by
Tommy Z
Top achievements
Rank 1
Answers by
Maya
Telerik team
Tommy Z
Top achievements
Rank 1
Share this question
or