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

no SourceField for EnumerableAggregateFunction

2 Answers 175 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Brian Graves
Top achievements
Rank 1
Brian Graves asked on 25 May 2010, 02:00 AM
I have written my own custom aggregate function to count distinct column values (since the CountFunction class doesn't offer this currently).  Unfortunately, there's is no SourceField property to set and I end up having to write a new count function for every single column in my grid.  How can I pass the column name (or SourceField) to my counting method?  Here's my code:

    public class CountDistinct : EnumerableAggregateFunction
    {
        private string _columnName;
        public CountDistinct(string columnName)
        {
            _columnName = columnName;
            //SourceField = columnName; DOESN'T EXIST
        }

        protected override string AggregateMethodName
        {
            get
            {
                return "CountDistinct";
            }
        }

        protected override Type ExtensionMethodsType
        {
            get
            {
                return typeof(CountAggregates);
            }
        }
    }

    public class CountAggregates
    {
        public static int CountDistinct<TSource>(IEnumerable<TSource> source)
        {
            Dictionary<string, string> dUniqueValues = new Dictionary<string, string>();

            foreach (TSource item in source)
            {
                PropertyInfo colNameProp = item.GetType().GetProperty(SourceField);
                string value = colNameProp.GetValue(item, null).ToString().ToLower();
                if (!dUniqueValues.ContainsKey(value))
                    dUniqueValues.Add(value, "whatever");
            }

            return dUniqueValues.Keys.Count;
        }
    }

2 Answers, 1 is accepted

Sort by
0
Brian Graves
Top achievements
Rank 1
answered on 26 May 2010, 12:40 AM
I ended up using the EnumerableSelectorAggregateFunction class to do this.  See link:
http://www.telerik.com/community/forums/silverlight/gridview/footer-sum-of-one-or-more-fields.aspx
0
Milan
Telerik team
answered on 26 May 2010, 10:07 AM
Hello Brian Graves,

Thank you for sharing your findings with the community.

I have updated your Telerik points.


All the best,
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.
Tags
GridView
Asked by
Brian Graves
Top achievements
Rank 1
Answers by
Brian Graves
Top achievements
Rank 1
Milan
Telerik team
Share this question
or