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:
- Create a class and implement the function.
- Register the function.
- Call the function in your application.
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.
To use base and advanced functionality, implement one or several of the following ICustomAggregate descendants:
- ICustomAggregateQueryable - Implement this interface to use custom aggregate functions in LINQ to XPO expressions.
- ICustomAggregateFormattable - Custom aggregate functions that implement this interface are evaluated on the database server.
- ICustomAggregateConvertibleToExpression - Implement this interface to support LINQ-based Server Mode data sources.
Register a Custom Aggregate Function
To enable a custom aggregate function, register it in the application. Choose one of the following methods:
- The CriteriaOperator.RegisterCustomAggregate(ICustomAggregate) method makes the registered custom aggregate function available across the entire application in all locations where the CriteriaOperator is available. This is the recommended approach.
- The ConnectionProviderSql.RegisterCustomAggregate(ICustomAggregateFormattable) method makes the custom aggregate function available for a specific database’s provider, and only within XPO.
If you need to register several functions, use the methods below:
- CriteriaOperator.RegisterCustomAggregates(IEnumerable<ICustomAggregate>)
- ConnectionProviderSql.RegisterCustomAggregates(ICollection<ICustomAggregateFormattable>)
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:
- CriteriaOperator.UnregisterCustomAggregate(ICustomAggregate)
- CriteriaOperator.UnregisterCustomAggregate(String)
- ConnectionProviderSql.UnregisterCustomAggregate(ICustomAggregateFormattable)
- ConnectionProviderSql.UnregisterCustomAggregate(String)
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.