Ways to Build Criteria
- 4 minutes to read
This topic demonstrates options for creating criteria when you use any of the filtering techniques in an XAF application.
A Criteria as a CriteriaOperator Object
When you are required to set a criteria as a CriteriaOperator object, you have the following options:
Use Criteria Operators provided by XPO. There are several XPO criteria operators that you can use to build the required criteria. The following code demonstrates how to build a simple criteria using the BinaryOperator:
using DevExpress.Data.Filtering; //... // 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);
Use the CriteriaOperator.Parse method. You can represent criteria as a human-readable string and parse it using the static CriteriaOperator.Parse method.
using DevExpress.Data.Filtering; //... //The criteria that represents a logical expression (City <> "Chicago") is represented by a string. CriteriaOperator criteria = CriteriaOperator.Parse("City != 'Chicago'");
To learn the syntax used in the CriteriaOperator.Parse method, refer to the Criteria Language Syntax help topic.
For details on how to build criteria using XPO techniques, refer to the following links:
A Criteria as a String
Anywhere you are required to specify a criteria as a string, you should set a string that can be parsed by the static CriteriaOperator.Parse method. To learn the syntax used in this method, refer to the Criteria Language Syntax help topic.
The places where you should specify a criteria as a string include properties in the Model Editor, and attributes and different properties and methods in code.
The following code demonstrates how to set criteria for the ActionBase.TargetObjectsCriteria property of an Action. The Controller below makes the Action available when the selected Task‘s DueDate property value is less than or equal to the system’s date and time:
public partial class MyController : ViewController {
private void MyController_AfterConstruction(object sender, EventArgs e) {
TargetObjectType = typeof(Task);
MyAction.SelectionDependencyType = SelectionDependencyType.RequireMultipleObjects;
MyAction.TargetObjectsCriteria = "DueDate <= LocalDateTimeNow()";
}
}
The following image demonstrates how the TargetObjectsCriteria property is specified in the Model Editor:
Get Criteria String via the Criteria Property Editor
XAF provides the following string property editors, intended to construct filter criteria visually (see Criteria Properties):
Windows Forms Criteria Property Editors:
- AdvancedCriteriaPropertyEditor
- CriteriaPropertyEditor
- ExtendedCriteriaPropertyEditor
- PopupCriteriaPropertyEditor
ASP.NET Web Forms Criteria Property Editors:
- ASPxCriteriaPropertyEditor
- ASPxPopupCriteriaPropertyEditor
These property editors can be used to represent string properties decorated with the CriteriaOptionsAttribute attribute. The following image illustrates the PopupCriteriaPropertyEditor:
The corresponding criteria string is:
[DueDate] <= LocalDateTimeNow() and [Status] = 'Completed'
The important concept is that the Criteria Property Editors can generate criteria strings containing the XAF-specific Object Parameters. Such criteria strings cannot be parsed using the CriteriaOperator.Parse method, provided by XPO. You should use the GetCriteriaOperator method exposed by the CriteriaEditorHelper helper class, to get the corresponding CriteriaOperator object:
CriteriaOperator criteria =
CriteriaEditorHelper.GetCriteriaOperator(criteriaString, dataType, objectSpace);
Refer to the How to: Use Criteria Property Editors topic to see full example. This topic also provides a list of current Criteria Property Editors limitations in ASP.NET Web Forms applications.