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

Timespan Sum Aggregation Function

11 Answers 438 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Adam
Top achievements
Rank 1
Adam asked on 22 Mar 2010, 11:58 AM
When I try to apply the Summ Aggregation function to a column containing Timespans I get the following exception:

"No generic method 'Sum' on type 'System.Linq.Enumerable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic."

My column declaration looks like this:

 

<telerikGridView:GridViewDataColumn Header="Duration" UniqueName="Duration">  
    <telerikGridView:GridViewDataColumn.AggregateFunctions> 
        <data:SumFunction Caption="Total: " /> 
    </telerikGridView:GridViewDataColumn.AggregateFunctions> 
</telerikGridView:GridViewDataColumn> 

 


Can anyone point me in the right direction or an example of a custom aggregation faunction, if that's what I need to do?

Thanks.

 

 

 

11 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 22 Mar 2010, 02:12 PM
Hi Adam,

RadGridView uses the standard extension methods like Sum, Average, etc to calculate the result of its aggregate function and if such methods are not defined an exception is thrown. 

The solution is to create a simple custom aggregate function to do the job:

public class TimeSpanSumFunction : EnumerableSelectorAggregateFunction
{
    protected override string AggregateMethodName
    {
        get
        {
            // which method of the ExtensionMethodsType will handle the summation
            return "Sum";
        }
    }
  
    // assign new type to handle summation
    protected override Type ExtensionMethodsType
    {
        get
        {
            return typeof(Statistics);
        }
    }
}
  
public static class Statistics
{
    public static TimeSpan Sum<T, TTimeSpan>(IEnumerable<T> source, Func<T, TimeSpan> selector)
    {
        return source.Select(selector).Aggregate(TimeSpan.Zero, (t1, t2) => t1 + t2);
    }
}

One the custom function is ready it is straightforward to use:

<telerik:GridViewDataColumn Header="Position" DataMemberBinding="{Binding Position}">
            <telerik:GridViewDataColumn.AggregateFunctions>
                <local:TimeSpanSumFunction SourceField="Position"/>
            </telerik:GridViewDataColumn.AggregateFunctions>
        </telerik:GridViewDataColumn>
</telerik:GridViewDataColumn>

Greetings,
Milan
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Camron
Top achievements
Rank 1
answered on 14 Jun 2010, 11:51 PM
Milan,

I am still encountering the problem Adam was encountering even after implementing your solution. Perhaps you can help? I am using Blend 4 Ultimate and VS2010 to develop a prototype.

My XAML:
    <Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource JobGLSummaryCurrent}}"
        <telerik:RadGridView DataContext="{Binding Collection}" ItemsSource="{Binding}" > 
            <telerik:RadGridView.Columns> 
                <telerik:GridViewDataColumn DataMemberBinding="{Binding JobNo}" Header="Job"/> 
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Type}" Header="Type"/> 
                <telerik:GridViewDataColumn DataMemberBinding="{Binding CostCode}" Header="Cost Type"/> 
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Actuals}" Header="Actuals (Inc. Accruals)" DataFormatString="{}{0:c}"
                    <telerik:GridViewDataColumn.AggregateFunctions> 
                        <local:SumFunction/> 
                    </telerik:GridViewDataColumn.AggregateFunctions> 
                </telerik:GridViewDataColumn> 
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Budget}" Header="Budget (Inc. CO's)"/> 
                <telerik:GridViewDataColumn DataMemberBinding="{Binding BudRem}" Header="Bud. Rem."/> 
                <telerik:GridViewDataColumn DataMemberBinding="{Binding BudPercentRem}" Header="% Bud. Rem."/> 
            </telerik:RadGridView.Columns> 
        </telerik:RadGridView> 
    </Grid> 

My C# classes:
    public class SumFunction : EnumerableSelectorAggregateFunction 
    { 
 
        protected override string AggregateMethodName 
        { 
            get { return "Sum"; } 
        } 
 
        protected override Type ExtensionMethodsType 
        { 
            get 
            { 
                return typeof (Amounts); 
            } 
        } 
    } 
 
    public static class Amounts 
    { 
        public static double Sum<T, TDouble>(IEnumerable<T> source, Func<T, System.Double> selector) 
        { 
            return source.Select(selector).Aggregate(Double.Parse("0"), (t1, t2) => t1 + t2); 
        } 
    } 

I'm using the latest version as of this post. The target version is Silverlight 4.

I still receive the error when I implement the solution you suggested. Once I take the Aggregate function out of the column, everything works fine. I am binding to a sample data source made in Blend. The DataType value of the GridViewDataColumn is "System.Double", which is automatically put in by Blend after I bind the collection I'm using.

Any help on this matter would be appreciated.


Camron
0
Vlad
Telerik team
answered on 15 Jun 2010, 06:27 AM
Hello Camron,

 Do you have our latest service pack (Q1 2010 SP2)? Please post more info about the grid version. 

Kind regards,
Vlad
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Camron
Top achievements
Rank 1
answered on 15 Jun 2010, 03:45 PM
Vlad,

I have "RadControls for Silverlight Q1 2010 SP2" installed. As a note, I also have "RadControls for WinForms Q1 2010" installed alongside (just in case that makes a difference).

I am using the following references:

Telerik.Windows.Controls
Telerik.Windows.Controls.Gridview
Telerik.Windows.Controls.Input
Telerik.Windows.Controls.Data

All are version 2010.1.603.1040 with a runtime version of v2.0.50727.


Camron
0
Vlad
Telerik team
answered on 15 Jun 2010, 03:50 PM
Hello Camron,

 Sorry - I've missed that SourceField is not specified. Please set this to desired property to avoid this error.

Kind regards,
Vlad
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Camron
Top achievements
Rank 1
answered on 15 Jun 2010, 03:56 PM
Vlad,

The error is still occuring.

 <Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource JobGLSummaryCurrent}}"
        <telerik:RadGridView DataContext="{Binding Collection}" ItemsSource="{Binding}" >  
            <telerik:RadGridView.Columns>  
                <telerik:GridViewDataColumn DataMemberBinding="{Binding JobNo}" Header="Job"/>  
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Type}" Header="Type"/>  
                <telerik:GridViewDataColumn DataMemberBinding="{Binding CostCode}" Header="Cost Type"/>  
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Actuals}" Header="Actuals (Inc. Accruals)">  
                    <telerik:GridViewDataColumn.AggregateFunctions>  
                        <local:SumFunction SourceField="Actuals"/>  
                    </telerik:GridViewDataColumn.AggregateFunctions>  
                </telerik:GridViewDataColumn>  
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Budget}" Header="Budget (Inc. CO's)"/>  
                <telerik:GridViewDataColumn DataMemberBinding="{Binding BudRem}" Header="Bud. Rem."/>  
                <telerik:GridViewDataColumn DataMemberBinding="{Binding BudPercentRem}" Header="% Bud. Rem."/>  
            </telerik:RadGridView.Columns>  
        </telerik:RadGridView>  
    </Grid> 

0
Vlad
Telerik team
answered on 15 Jun 2010, 04:00 PM
Hello Camron,

 Can you send us an example project (via support ticket) demonstrating this problem? 

Sincerely yours,
Vlad
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
David Cronin
Top achievements
Rank 1
answered on 15 Jun 2010, 08:23 PM
Vlad,

The ticket number is 319174. I had to submit it via our development account.

Camron
0
Vlad
Telerik team
answered on 16 Jun 2010, 06:00 AM
Camron,

 I saw the ticket however in your project your are trying to call Sum for Property1 which is string. You cannot do that.

Regards,
Vlad
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Maya
Telerik team
answered on 17 Jun 2010, 07:59 AM
Hello David Cronin,

The problem here is in the definition of your custom function. So, instead of defining it as follow:

public static class Statistics
   {
       public static Double Sum<T, TDouble>(IEnumerable<T> source, Func<T, Double> selector)
       {          
        return source.Select(selector).Aggregate(Double.MaxValue, (t1, t2) => t1 + t2);
       }
   }

You need to remove the second parameter in Sum<>:
public static class Statistics
    {
        public static Double Sum<T>(IEnumerable<T> source, Func<T, Double> selector)
        {          
         return source.Select(selector).Aggregate(Double.MaxValue, (t1, t2) => t1 + t2);
        }
    }


 

All the best,
Maya
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Camron
Top achievements
Rank 1
answered on 17 Jun 2010, 03:50 PM
Thank you, Maya. The problem is no longer present. Thank you for your support!
Tags
GridView
Asked by
Adam
Top achievements
Rank 1
Answers by
Milan
Telerik team
Camron
Top achievements
Rank 1
Vlad
Telerik team
David Cronin
Top achievements
Rank 1
Maya
Telerik team
Share this question
or