"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
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.
<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> |
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); |
} |
} |
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.
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.
<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> |
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.
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.
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.