Custom Aggregate Function: AggDistinctAvg()
Review the documentation on Custom Aggregate Functions for full details on developing and implementing this function.
| Description | Returns the average of only unique values in a series of values. | 
|---|---|
| Arguments | 
 | 
| Namespaces | 
 | 
| References | 
 | 
| Example | AggDistinctAvg({OrderDetails.Quantity}) | 
Program Code
public class AggDistinctAvg : ICustomAggregator
{
    HashSet<Decimal> list = new HashSet<Decimal>();
  
	public void AddValue(SessionInfo sessionInfo, object value, params object[] args)
	{
		Decimal curVal;
  
        if (value == null)
            curVal = 0;
        else if (!Decimal.TryParse(value.ToString(), out curVal))
        {
            throw new WrAggregationException(@"Tried to take the average of a set
                                              containing a non-number");
        }
  
        this.list.Add(curVal);
	}
	public object Result(SessionInfo sessionInfo)
	{
        Decimal sum = 0;
        foreach (Decimal val in list)
        {
            sum += val;
        }
        return sum/this.list.Count; // should never be 0
	}
}