Skip to main content

Custom Aggregate Functions

  • 3 minutes to read

Overview

The DevExpress Data Library supports standard aggregate functions (Min, Max, Sum, Avg, Single, Exists, and Count) and custom aggregate functions.

To use custom aggregate functions, you need to:

For more details on how to implement and use custom aggregate functions, refer to the How to: Implement and Use Custom Aggregate Functions article and the How to Implement Custom Aggregates example.

Implement a Custom Aggregate Function

Define a class that implements the ICustomAggregate interface to create an aggregate that accepts a collection of values, uses an expression or several expressions to evaluate the values, and returns one result.

public class CountDistinctCustomAggregate : ICustomAggregate {
}

To use base and advanced functionality, implement one or several of the following ICustomAggregate descendants:

Register a Custom Aggregate Function

To enable a custom aggregate function, register it in the application. Choose one of the following methods:

If you need to register several functions, use the methods below:

When you no longer need the custom aggregate function or need to hide it from the UI, you can unregister it from the application with one of the corresponding methods below:

Call a Custom Aggregate Function in Criteria Operator Syntax

To use a custom aggregate function, you can call it directly:

[CollectionProperty][].MyCustomAggregate(property)

Alternatively, you can pass the custom aggregate function’s name as one of the parameters:

[CollectionProperty][].AGGREGATE('MyCustomAggregate', property)

An aggregate is considered top-level if it is applied to the parent collection. If a custom aggregate is top-level, use the following syntax:

[].MyCustomAggregate(property)

[].AGGREGATE('MyCustomAggregate', property)

XPO supports custom aggregates with any number of arguments or expressions - as compared to standard aggregates that accept one or no arguments, like Sum(value) or Count().

As a best practice, capitalize .AGGREGATE calls.

Limitations

Server Mode

Custom aggregate functions are not supported in server mode. You cannot calculate the value of a property in server mode using a custom aggregate function.

See Also