[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Reporting
[Expand]Report and Dashboard Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Collapse]eXpress Persistent Objects
 [Expand]Product Information
 [Expand]Getting Started
 [Expand]Fundamentals
 [Expand]Feature Center
 [Expand]Concepts
 [Expand]Design-Time Features
 [Collapse]Examples
   How to: Access Data in SQL Query Results
   How to: Bind an XPCollection to a LookUp
   How to: Bind an XPCollection to Standard Controls
   How to: Bind an XPCollection to the DataGrid
   How to: Bind an XPCollection to the Grid
   How to: Build Complex Criteria
   How to: Build Simple Criteria
   How to: Change Inheritance Mapping
   How to: Connect to a SQL Server
   How to: Connect to SQL Azure
   How to: Connect XPO to a Database Other than MS SQL or MS Access
   How to: Control Automatic Saving of Objects when Editing in a Grid
   How to: Create an Aggregated Object
   How to: Delay Property Loading
   How to: Filter By DateTime Values
   How to: Filter Persistent Objects by Type
   How to: Handle Persistent Object Initialization
   How to: Implement a Custom Criteria Language Function Operator
   How to: Implement a Full-Text Search
   How to: Implement Custom Functions and Criteria in LINQ to XPO
   How to: Implement Many-to-Many Relationships
   How to: Implement One-to-One Relationships
   How to: Link Classes Located in Different Assemblies
   How to: Make a Class or Property Non-Persistent
   How to: Make XPCollection Create Objects of Specific Type when They are Created via Bound Controls
   How to: Map to Custom Tables (Views) and Columns
   How to: Obtain a Collection of Persistent Objects by a Set of Their IDs
   How to: Obtain a Collection of Persistent Objects for Processing
   How to: Persist a BLOB Field
   How to: Prevent Changes in a Persistent Object from being Automatically Committed
   How to: Restore Deleted Objects
   How to: Use Pageable Collections
   How to: Use Read-Only Persistent Properties
   How to: Use the ContainsOperator for Objects in a Many-to-Many Relationship
   How to: Validate an Object
   How to: Work with Structures
 [Expand]Member Tables
 [Expand]API Reference
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

How to: Implement Custom Functions and Criteria in LINQ to XPO

To make Custom Functions or custom criteria available in LINQ to XPO expressions, all that you need to do is add a corresponding interface implementation (ICustomFunctionOperatorQueryable or ICustomCriteriaOperatorQueryable) to your function class declarations. For custom criteria, you need to also extend the registration code with the CustomCriteriaManager.RegisterCriterion or CustomCriteriaManager.RegisterCriteria method call (see details below).

Click one of the following links to quickly navigate to the desired section of the article.

Expanded Implementing Custom Functions

The code sample below demonstrates how to define the MyConcat custom function by implementing the ICustomFunctionOperatorQueryable interface. Take special note of the static Register and Unregister methods we added to simplify the function's registration.

To register the MyConcat custom function, call its Register method. If you do not need this custom function anymore, call its Unregister method.

The MyConcat function is ready now to be used in LINQ to XPO expressions.

Note that the MyConcatFunction.MyConcat function requires four parameters, rather than a parameter array, which is more convenient. A fixed parameter list is a restriction for ICustomFunctionOperatorQueryable interface implementations - they cannot handle parameter arrays passed as function parameters because LINQ returns these arrays as one argument to a custom function. To overcome this restriction, implement custom criteria with which you can handle any number of function parameters using parameter arrays, and create a corresponding CriteriaOperator object.

Expanded Implementing Custom Criteria

Unlike custom functions, custom criteria allow you to substitute custom function calls in LINQ to XPO expressions with CriteriaOperator counterparts, which are then evaluated to determine the return value. Custom criteria declarations are similar to custom function declarations, except that the ICustomFunctionOperatorQueryable interface is replaced with ICustomCriteriaOperatorQueryable, and the registration code is extended with corresponding CustomCriteriaManager method calls.

Consider the extension of the MyConcat function implemented above with the capability to accept an array as its parameter with the help of custom criteria. The only changes in the MyConcatFunction declaration are as follows.

The MyConcat function now accepts any number of parameters.

Expanded See Also

Is this topic helpful?​​​​​​​