Back to Devexpress

How to: Build Simple Criteria

xpo-2132-examples-how-to-build-simple-criteria.md

latest4.6 KB
Original Source

How to: Build Simple Criteria

  • Oct 03, 2022
  • 3 minutes to read

Using criteria objects you can precisely specify the objects that you want to appear in a collection. Criteria is a tree of objects representing a logical expression. Generally, a simple logical expression is comprised of a relational operator and its operands.

This topic demonstrates how to represent simple logical expressions in code. Please refer to How to Build Complex Criteria which is to do with complex logical expressions.

Building Simple Criteria - Example

With XPO, you have multiple options for building simple filter criteria. For instance, the same criteria which represents a simple logical expression (City <> “Chicago”) can be represented using two different notations as shown in code examples below.

csharp
// The criteria that represents a logical expression (City <> "Chicago") 
// is represented by the BinaryOperator and two operands.
CriteriaOperator criteria = new BinaryOperator(
    new OperandProperty("City"), new OperandValue("Chicago"),
    BinaryOperatorType.NotEqual
);
vb
' The criteria that represents a logical expression (City <> "Chicago") 
' is represented by the BinaryOperator and two operands.
Dim criteria As CriteriaOperator = New BinaryOperator( _
    New OperandProperty("City"), New OperandValue("Chicago"), _
    BinaryOperatorType.NotEqual _
)

The main drawback of this way of writing criteria is that you have to write a lot of code. It can also be quite difficult to come up with complete hierarchies of operators that are frequently needed for complex expressions.

csharp
// The criteria that represents a logical expression (City <> "Chicago") is represented by a string.
CriteriaOperator criteria = CriteriaOperator.Parse("City != 'Chicago'");
vb
' The criteria that represents a logical expression (City <> "Chicago") is represented by a string.
Dim criteria As CriteriaOperator = CriteriaOperator.Parse("City != 'Chicago'")

The number of criteria operators available in XPO allow you to easily form logical expressions of considerable complexity.

The drawback here is that complex expressions basically stored in string variables in the code are not checked. If you are creating operator classes in code, it’s possible to make mistakes in field names, or comparing things that naturally can’t be compared. The whole string containing a query expression can’t be checked, and therefore can easily bring up runtime errors.

See Also

How to: Build Complex Criteria

Build Criteria - Cheat Sheet

How to: Use Read-Only Persistent Properties

PersistentAttribute

Filtering.CriteriaOperator

AggregateOperand

BetweenOperator

BinaryOperator

BinaryOperatorType

ContainsOperator

FunctionOperator

FunctionOperatorType

GroupOperator

GroupOperatorType

InOperator

NotOperator

NullOperator

OperandProperty

OperandValue

UnaryOperator

UnaryOperatorType