Progress® Telerik® Reporting R2 2017

User Aggregate Functions

User aggregate functions allow you to apply custom logic when accumulating values over a set of rows from the data source. They are used by the Telerik Reporting engine as all built-in aggregate functions.

Implementing a custom aggregate function

User aggregates are public or internal (Public or Friend in VB.NET) classes that implement the IAggregateFunction interface.

Aggregate function implementation accumulates values from each row using the Accumulate method. The aggregate can take an arbitrary number of input parameters and will receive them as items in the object array parameter passed in this method.

The function merges its value with other instances of the aggregate using the Merge method. For example if you implement SUM aggregate and merge an instance of the aggregate with current accumulated value of 4+5=9 within the current aggregate instance with accumulated value of 1+2+3=6 the result should be 15.

The function returns value using the GetValue method.

Apply AggregateFunctionAttribute to the custom aggregate class implementation to define an interface for the users of the aggregate function. The Name parameter of the attribute defines how to refer to the function in expressions.

Example

[AggregateFunction(Description = "Concatenation aggregate. Output: (value1, value2, ...)", Name = "Concatenate")]
class ConcatenateAggregate : IAggregateFunction
{
    string result;

    public void Accumulate(object[] values)
    {
        // The aggregate function expects one parameter
        object value = values[0];

        // null values are not aggregated
        if (null == value)
        {
            return;
        }

        // The actual accumulation
        if (this.result.Length > 0)
        {
            result += ", ";
        }
        this.result += value.ToString();
    }

    public object GetValue()
    {
        return string.Format("({0})", this.result);
    }

    public void Init()
    {
        // Add aggregate function initialization code here if needed
        this.result = string.Empty;
    }

    public void Merge(IAggregateFunction aggregateFunction)
    {
        ConcatenateAggregate aggregate = (ConcatenateAggregate)aggregateFunction;

        if (aggregate.result.Length > 0)
        {
            if (this.result.Length > 0)
            {
                result += ", ";
            }
            this.result += aggregate.result;
        }
    }
}

Invoking a Custom Aggregate Function

You can use a custom aggregate within expressions the same way you invoke an built-in aggregate function:

=Concatenate(Fields.ProductName)

Note Note

Custom aggregates are not supported when you preview the report in Visual Studio. To see the aggregate output, use a ReportViewer control.