Custom Functions
- 2 minutes to read
Overview
Expressions support multiple predefined functions.
- Aggregate Functions (for example, average value).
- Date-time Functions (for example, is today).
- Logical Functions (for example, is null or empty).
- Math Functions (for example, absolute value).
- String Functions (for example, starts with).
These functions allow you to calculate values for unbound columns, implement conditional formatting or create complex filters. For example, you can only show orders that exceed the average cost and are shipped today. See Criteria Language Syntax for more information.
You can also create a custom function. For example, you can implement not starts with function.
How to Implement a Custom Function
To create a custom function, implement one of these interfaces:
- ICustomFunctionOperator - a basic custom function.
- ICustomFunctionOperatorBrowsable - a custom function accessible in expression editors.
ICustomFunctionDisplayAttributes
- a custom function accessible in expression editors, filter editors and filter menus.- ICustomFunctionOperatorFormattable - a custom function that can be processed by an SQL database. Works only with XPO data sources.
- ICustomFunctionOperatorConvertibleToExpression - a custom function that can be converted to a LINQ expression. Required for IQueryable-based Server Mode data sources.
Important
Do not use the function name that starts with Is
if this function does not return a Boolean value. It may lead to unexpected results.
Examples
- How to: Implement a Custom Criteria Language Function Operator — Demonstrates how to implement a function that extracts the month from a date-time value.
- How to: Use a Custom Function in a Query Expression — Demonstrates how to create a function that calculates the standard deviation.
How to Register a Custom Function
A custom function should be registered in the system to make it available in expressions and/or database queries. To register a function, do one of the following:
Pass the function to the static (Shared in VB) CriteriaOperator.RegisterCustomFunction or CriteriaOperator.RegisterCustomFunctions method.
This makes the function available in expressions and database queries created with XPO.
Pass the function to a data storage provider’s RegisterCustomFunctionOperator method and add it to a dictionary’s XPDictionary.CustomFunctionOperators collection.
This makes the function available in XPO queries based on dictionary entries only.
To unregister a custom function, call the CriteriaOperator.UnregisterCustomFunction method.
Note
Refer to How to: Implement Custom Functions and Criteria in LINQ to XPO for more information on how to implement custom functions and use them in LINQ expressions created with XPO.