Skip to main content
A newer version of this page is available. .

Custom Function Criteria Operators

  • 3 minutes to read

Built-in Function Criteria Operators cover the most common data management scenarios. Additionally, you can define custom Function Criteria Operators for those situations when the built-in Operators do not suit your needs. For example, if you are frequently using a particular expression, and do not want to type it over and over again, you can implement a custom Function Criteria Operator. A custom Function Criteria Operator is defined by a class implementing the ICustomFunctionOperator interface. The interface exposes three members allowing you to specify a custom function and evaluate its value on the client side. The Name property specifies the name of the custom Operator. The ResultType method calculates the return type of the custom Function Criteria Operator, based on the types of passed arguments. The Evaluate method calculates the Function Criteria Operator’s value based on the passed arguments.

public class WeekAgoOperator : ICustomFunctionOperator {
    public string Name {
        get { return "WeekAgo"; }
    }
    public object Evaluate(params object[] operands) {
        return DateTime.Today.AddDays(-7);
    }
    public Type ResultType(params Type[] operands) {
        return typeof(DateTime);
    }
}

To use a custom Function Criteria Operator, you need to register it. For this purpose, first add a static constructor to the Operator. In the constructor, invoke the CriteriaOperator.RegisterCustomFunction method. Placing this method call into the static constructor ensures that the Operator will not be registered twice accidentally.

using DevExpress.Data.Filtering;
//...
public class WeekAgoOperator : ICustomFunctionOperator {
    //...
    static WeekAgoOperator() {
        WeekAgoOperator instance = new WeekAgoOperator();
        if (CriteriaOperator.GetCustomFunction(instance.Name) == null) {
            CriteriaOperator.RegisterCustomFunction(instance);
        }
    }
    public static void Register() { }
}

Then, invoke this constructor in a module constructor.

public sealed partial class MyModule : ModuleBase {
    public MyModule() {
        WeekAgoOperator.Register();
    }
}

After registering a Function Criteria Operator, you can use it anywhere it is required - in List View filters, Validation and Appearance rules, object-level security permissions, etc. For example, you can create a List View filter via the Filter Action that will use your Operator, i.e. select only the objects that were shipped within the last seven days.

ShippingDate > WeekAgo()

If your custom Function Criteria Operator is going to be used in a server-side filtering, support the ICustomFunctionOperatorFormattable interface as well.

Note that a custom Function Criteria Operator can be used in several applications. So, it is recommended that you implement all necessary custom Function Criteria Operators in a separate module.

Note

There is an example of how to create a custom Function Criteria Operator in the DevExpress Code Central database at https://supportcenter.devexpress.com/ticket/details/e3945/how-to-create-a-custom-function-criteria-operator. Depending on the target platform type (ASP.NET Web Forms, WinForms, etc), you can either run this example online or download an auto-executable sample.