how to custom the aggregate function

4 posts, 0 answers
  1. Ocean Ju
    Ocean Ju avatar
    7 posts
    Member since:
    Mar 2010

    Posted 13 Dec 2011 Link to this post

    I read the example : and defined a class like this:

    using System;
    using Telerik.Windows.Data;
    using System.Collections.Generic;
    using System.Linq;

    namespace MSHF.Paris.UIPlatform.Common
        public class ObjectSumFunction : EnumerableSelectorAggregateFunction
            protected override string AggregateMethodName
                    return "Sum";

            protected override Type ExtensionMethodsType
                    return typeof(ObjectSum);

        public static class ObjectSum
            public static object Sum<TSource>(IEnumerable<TSource> source, Func<TSource, object> selector)
                int itemCount = source.Count();
                if (itemCount > 0)
                    IEnumerable<decimal> values = from i in source select Convert.ToDecimal(selector(i));

                    return values.Sum();

                return 0;

    add the function in code:
    dataColumn.AggregateFunctions.Add(new ObjectSumFunction()
                                Caption = "total:",
                                SourceField = "A4",
                                SourceFieldType = typeof(object),
                                ResultFormatString = gd.GridColumnDefinitions[i].DataFormat

    the column type is object(in fact, it is a decimal, but converted to object)

    Get the error:No generic method 'Sum' on type 'MSHF.Paris.UIPlatform.Common.ObjectSum' is compatible with the", because it is a Just-In-Time Debugger dialog, the message is trucated.
    please see the attached error file.

  2. Ocean Ju
    Ocean Ju avatar
    7 posts
    Member since:
    Mar 2010

    Posted 19 Dec 2011 Link to this post

    any suggestion?
  3. Igor
    Igor avatar
    26 posts
    Member since:
    Nov 2010

    Posted 20 Apr 2012 Link to this post

    Do you have tried this: 
    Func<TSource, decimal> instead of  Func<TSource, Object> ?
    I have the same problem with enum and string types. I don't know which types are available to the selector, but it seam that  string, Object and Enum aren't supported. This is a limitation in my project ,because my grid row data doesn't have numeric fields, only enums and strings. I have to add a dummy integer field only to make the function callable.
    In my application I want to report how many rows are orangutan or chimpanzee counting the field ItemType grouped by location.
    I use this code:
    public static string CountPrimate<T>(IEnumerable<T> source
        , Func<T, int> selector) where T : PrimateViewModel
        int  oranCount = source.Count(x =>
            x.ItemType == PrimateTypeEnum.ORAN);
        int chimCount  = source.Count(x =>
            x.ItemType == PrimateTypeEnum.CHIM);
        return string.Format("Total Chimpanzee: {0}  Orangutan: {1}", chimCount, oranCount);

    <telerik:GridViewDataColumn DataMemberBinding="{Binding ItemType}">
            <local:CountPrimateFunction Caption="" SourceField="DummyInt" />
        <telerik:GroupDescriptor Member="Location.Name"  >

    The selector is not used because I don't have any integer fields (only the dummy one)  and using where T : PrimateViewModel 
     I can access to all source fields.  Returning a string I don't need the caption.
    Why is the selector limited to numeric source fields? Is there a better way to solve this aggregation problem?
  4. Dimitrina
    Dimitrina avatar
    3769 posts

    Posted 21 Apr 2012 Link to this post


     If the custom aggregates do not work in your case, then I would recommend you to try the approach suggested in this forum thread.

    the Telerik team

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

Back to Top