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

Aggregate functions - using Sum gives an exception with complex objects

6 Answers 346 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Julien
Top achievements
Rank 1
Julien asked on 08 Aug 2012, 09:00 AM
Hi everybody,

I'm struggling for hours using the aggregate functions in my datagrid.
I'm using Telerik RadControls WPF Q2 2012 SP1.

Here is the code:

Xaml:
<Telerik:RadGridView x:Name="uxGratuitiesDataGrid"
                                     Margin="-11,-11,-11,0"
                                     Grid.Row="0"
                                     IsReadOnly="True"
                                     CanUserSortColumns="True"
                                     CanUserFreezeColumns="False"
                                     CanUserReorderColumns="False"
                                     AutoExpandGroups="False"
                                     ColumnWidth="*"
                                     AutoGenerateColumns="False"
                                     RowIndicatorVisibility="Collapsed"
                                     ShowGroupPanel="False"
                                     ShowColumnFooters="True"
                                     IsFilteringAllowed="False"
                                     ItemsSource="{Binding SelectedGroupBooking.DailyBreakDown, Source={StaticResource Presenter}}">
                    <Telerik:RadGridView.Columns>
 
                        <!-- Gratuity amount -->
                        <Telerik:GridViewDataColumn
                            DataMemberBinding="{Binding Gratuity.BaseAmount, Converter={StaticResource AmountConverter}, UpdateSourceTrigger=PropertyChanged}"
                                                    Header="Base Amount">
                        </Telerik:GridViewDataColumn>
                        <!-- Total Gratuity amount -->
                        <Telerik:GridViewDataColumn DataMemberBinding="{Binding Gratuity.TotalAmount, Converter={StaticResource AmountConverter}}"
                                                    Header="Total Amount">
                            <Telerik:GridViewDataColumn.AggregateFunctions>
                                <Telerik:SumFunction SourceField="Gratuity.TotalAmount"/>
                            </Telerik:GridViewDataColumn.AggregateFunctions>
                        </Telerik:GridViewDataColumn>
 
                    </Telerik:RadGridView.Columns>


My collection is a set of objects defined as this:

public ObservableCollection<VomRoomStayBreakDown> DailyBreakDown
        {
            get { return _dailyBreakDown; }
            set { _dailyBreakDown = value; OnPropertyChanged("DailyBreakDown"); }
        }

and the VomRoomStayBreakDown contains this:


        public VomGratuity Gratuity
        {
            get { return _gratuity; }
            set { _gratuity = value; OnPropertyChanged("Gratuity"); }
        }

Which itself contains this:


        
public FldInteger Quantity
        {
            get { return _quantity; }
            set { _quantity = value; OnPropertyChanged("Quantity"); }
        }
 
 
        public VomAmount BaseAmount
        {
            get { return _baseAmount; }
            set { _baseAmount = value; OnPropertyChanged("BaseAmount"); }
        }
 
        public VomAmount TotalAmount
        {
            get { return _totalAmount; }
            set { _totalAmount = value; OnPropertyChanged("TotalAmount"); }
        }


Amount is composed of a decimal (Value) and a currency (Currency).

I am trying to Sum the decimal values from my VomAmounts (correctly displayed in the columns), but it always lead to the 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."

Can somebody tells me what I am doing wrong over there ?

Thanks a lot !

6 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 08 Aug 2012, 09:04 AM
Hello,

 I believe that in your case it will be better to use custom sum function similar to this demo:
http://demos.telerik.com/silverlight/#GridView/ExpressionColumn 

Kind regards,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Julien
Top achievements
Rank 1
answered on 08 Aug 2012, 09:14 AM
Hi Vlad,

Thanks a lot for your very fast answer. I've done this, using your advice:

public class MyTotalFunction : AggregateFunction<VomAmount, VomAmount>
    {
        public MyTotalFunction()
        {
            this.AggregationExpression = products => new VomAmount()
            {
                Value = new FldDecimal
                    (products.Sum(p => p.Value.Value)),
                Currency = new FldCurrencyCode("EUR")
            };
            this.ResultFormatString = "Total: {0:C}";
        }
    }

and using it this way:

<Telerik:GridViewDataColumn DataMemberBinding="{Binding Gratuity.TotalAmount, Converter={StaticResource AmountConverter}}"
                                                   Header="Total Amount">
                           <Telerik:GridViewDataColumn.AggregateFunctions>
                               <local:MyTotalFunction />
                           </Telerik:GridViewDataColumn.AggregateFunctions>
                       </Telerik:GridViewDataColumn>


Unfortunately this leads to another exception:

Expression of type 'System.Linq.IGrouping`2[System.Int32,VomRoomStayBreakDown]' cannot be used for parameter of type 'System.Collections.Generic.IEnumerable`1[VomAmount]' of method 'System.Decimal Sum[VomAmount](System.Collections.Generic.IEnumerable`1[VomAmount], System.Func`2[VomAmount,System.Decimal])'




0
Julien
Top achievements
Rank 1
answered on 08 Aug 2012, 09:45 AM
Hi, Submitted support ticket with code attachment.
Your ticket ID is: 574326 

0
Vlad
Telerik team
answered on 08 Aug 2012, 10:19 AM
Hi,

 I've reviewed your project and I've noticed that you can actually use the built-in Sum function however you need to provide:
 <Telerik:SumFunction SourceField="Gratuity.TotalAmount.Value"/>

since you can sum only numeric types - not custom types.

Kind regards,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Vlad
Telerik team
answered on 08 Aug 2012, 10:27 AM
Hello,

 Just a quick follow up. 

You can also rewrite your custom aggregate function as follows:

    public class MyTotalFunction : AggregateFunction<VomLine, decimal>
    {
        public MyTotalFunction()
        {
            this.AggregationExpression = item => item.Where(i=>i.Gratuity != null).Sum(i => i.Gratuity.TotalAmount.Value);


            this.ResultFormatString = "Total: {0:C}";
        }
    }

If you want you can use it instead the built-in Sum function. 

Kind regards,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Julien
Top achievements
Rank 1
answered on 08 Aug 2012, 11:13 AM
Hi Vlad,

Indeed, both methods work. I had simply forgotten to go down one more level in my object to obtain the decimal value. Beginer's mistake !

Thanks a lot for your help,

Regards,

Julien.
Tags
GridView
Asked by
Julien
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Julien
Top achievements
Rank 1
Share this question
or