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;
}
}