Quantcast
Channel: DevExpress Support Center (Examples)
Viewing all articles
Browse latest Browse all 7205

How to use a custom function in a query expression

$
0
0

This example illustrates how to use a custom aggregate function in a SELECT query.

In this example, the created custom function estimates the standard deviation (similar to the “stdev” function available in Microsoft SQL).

To provide this functionality, do the following.

1. Implement a custom aggregate function.

A custom function must implement the following interfaces (all belonging to the DevExpress.Data.Filtering namespace): ICustomFunctionOperator, ICustomFunctionOperatorBrowsable, and ICustomFunctionOperatorFormattable.

These interfaces provide the following main properties:

ICustomFunctionOperator:

- ResultType– specifies the type of a result corresponding to the input data of a specific type;

- Name– the name of a function, by which it can be addressed later on.

ICustomFunctionOperatorBrowsable:

- Category– the type of a function (logical, string, or math);

- Description– a short text description of the function;

- IsValidOperandCount– indicates whether or not calling this function with a specific number of parameters is allowed;

- IsValidOperandType– indicates whether or not a parameter supports the specific type (i.e., for the parameter with the specified operandIndex from operandCount);

- MaxOperandCount– specifies the maximum allowed number of parameters;

- MinOperandCount– specifies the minimum allowed number of parameters;

ICustomFunctionOperatorFormattable:

- Format– specifies how to construct an SQL string corresponding to this function (the providerType parameter allows you to determine the type of a connected server and generate appropriate strings for different dialects of SQL. In this example, this option is omitted, as the data source type is known beforehand).

 

2. Register this function.

To register custom functions in this example, the static RegisterCustomFunction method of the CriteriaOperator class is used.

For convenience, the custom function that has been created in the previous step implements the static Register method that registers the function. This method should be called before using the function for the first time (e.g., at the application launch). It does not matter if, for any reason, this method is called once again later on.

 

3. Use this function.

As the function has been registered, it can be used as a part of an SQL expression and becomes listed in the query editor among other functions.


Viewing all articles
Browse latest Browse all 7205

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>