Issue with adding aggregate function at runtime.

11 posts, 0 answers
  1. Jan
    Jan avatar
    24 posts
    Member since:
    Nov 2011

    Posted 02 Mar 2012 Link to this post

    I am getting this error when adding aggregate culumn at run time;

    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.

    private void SearchGrid_AutoGeneratingColumn(object sender,
                                                 Telerik.Windows.Controls.GridViewAutoGeneratingColumnEventArgs e)
    {
        if (string.IsNullOrEmpty(ReportWizardViewModel.ReportDefinition.Summary)) return;
     
        var fields = ReportWizardViewModel.ReportDefinition.Summary.Split(new char[] { ',' });
     
     
        var dataColumn = e.Column as GridViewDataColumn;
        if (dataColumn != null)
        {
            var columnName = dataColumn.Header.ToString();
            var fieldDef = fields.FirstOrDefault(w => w.StartsWith(columnName));
            if (fieldDef != null)
            {
                var pref = fieldDef.Replace(columnName + "-", "");
                dataColumn.DataMemberBinding.StringFormat = "{0:" + pref + "}";
     
     
                dataColumn.AggregateFunctions.Add(new SumFunction()
                            {
                                Caption = columnName + " :",
                                SourceField = columnName,
                                ResultFormatString = "{}{0:c2}",
                                SourceFieldType = typeof(decimal)
     
                            });
            }
     
     
        }
    }
  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 02 Mar 2012 Link to this post

    Hello,

     Can you check if you have numeric property with such name in your objects bound to grid?

    All the best,
    Vlad
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. Jan
    Jan avatar
    24 posts
    Member since:
    Nov 2011

    Posted 02 Mar 2012 Link to this post

    Yes, I do
    I am not sure, I am using a dynamic solution one of you guys posted. Usinng Wcf service generate DataSet to MyDataRow
    here is code;

    //client
     var client = new ReportServiceClient();
                        client.GetDataCompleted += (s, e) =>
                        {
                            if (e.Error != null)
                            {
                                var error = e.Error.Message;
                                RadWindow.Alert(error);
                                return;
                            }
     
     
                            Data = new ObservableCollection<MyDataRow>(from i in e.Result
                                                                        select
                                                                            new MyDataRow(i));
     
                            RaisePropertyChanged("Data");
                        };
                        client.GetDataAsync(ReportDefinition.Sql);

    where MyDataRow
    public class MyDataRow : DynamicObject, INotifyPropertyChanged
    {
        IDictionary<string, object> data;
     
        public MyDataRow()
        {
            data = new Dictionary<string, object>();
        }
     
        public MyDataRow(IDictionary<string, object> source)
        {
            data = source;
        }
     
        public override IEnumerable<string> GetDynamicMemberNames()
        {
            return data.Keys;
        }
     
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = this[binder.Name];
     
            return true;
        }
     
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            this[binder.Name] = value;
     
            return true;
        }
     
        public object this[string columnName]
        {
            get
            {
                if (data.ContainsKey(columnName))
                {
                    return data[columnName];
                }
     
                return null;
            }
            set
            {
                if (!data.ContainsKey(columnName))
                {
                    data.Add(columnName, value);
     
                    OnPropertyChanged(columnName);
                }
                else
                {
                    if (data[columnName] != value)
                    {
                        data[columnName] = value;
     
                        OnPropertyChanged(columnName);
                    }
                }
            }
        }
     
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
     
        #region INotifyPropertyChanged Members
     
        public event PropertyChangedEventHandler PropertyChanged;
     
        #endregion
    }

  4. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 02 Mar 2012 Link to this post

    Hello,

     Can you post the property declaration? 

    Kind regards,
    Vlad
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  5. Jan
    Jan avatar
    24 posts
    Member since:
    Nov 2011
  6. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 02 Mar 2012 Link to this post

    Hello,

     I'm afraid that standard Sum will not work for dynamics. You will need to create custom aggregate function - please check our demos for more info about various custom aggregates.  

    Regards,
    Vlad
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  7. Jan
    Jan avatar
    24 posts
    Member since:
    Nov 2011

    Posted 02 Mar 2012 Link to this post

    Thanks,
    Getting this now, Do not know how to fix this
    No generic method 'MySum' on type 'HiscoBudget.Controls.CustomSumCalc' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.

    client code changes
     
     dataColumn.AggregateFunctions.Add(new CustomSum()
                                    {
                                        Caption = columnName + " :",
                                        SourceField = columnName,
                                        ResultFormatString = "{}{0:c2}"
     
                                    });



    public class CustomSum : EnumerableSelectorAggregateFunction
     {
         protected override string AggregateMethodName
         {
             get { return "MySum"; }
         }
     
         protected override Type ExtensionMethodsType
         {
             get { return typeof(CustomSumCalc); }
         }
     
     }
     
     public static class CustomSumCalc
     {
         public static double MySum<TSource>(IEnumerable<TSource> source, Func<TSource, double> selector)
     
         {
             int itemCount = source.Count();
             if (itemCount > 1)
             {
                 IEnumerable<double> values = from i in source select Convert.ToDouble(selector(i));
                 return values.Sum();
             }
             return 0;
         }
     }





  8. Manoj
    Manoj avatar
    1 posts
    Member since:
    Jan 2012

    Posted 04 Mar 2012 Link to this post

    I am also struggling to find a solution for same. It is very critical and need to have solution by today. Could you please post the solution ASAP?
  9. Jan
    Jan avatar
    24 posts
    Member since:
    Nov 2011

    Posted 09 Mar 2012 Link to this post

    Well, I am still not able to find a solution. Vlad's article is a good one but need a better opion to generate or reflect data types so filtering and aggregate function can work
  10. Thomas
    Thomas avatar
    19 posts
    Member since:
    Sep 2010

    Posted 14 Mar 2012 Link to this post

    Can someone help with this one,
    No generic method 'MySum' on type 'HiscoBudget.Controls.CustomSumCalc' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.

  11. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 15 Mar 2012 Link to this post

    Hello,

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

    Regards,
    Didie
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Back to Top