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

Tutorial 3 - Querying Data

  • 4 minutes to read

As mentioned in Tutorial 1, persistent objects are usually retrieved from a database using the XPCollection or XPView. eXpress Persistent Objects allow you to specify the objects that appear in a collection by specifying logical expressions to be applied as a data filter.

In this tutorial, you will learn how to build your own simple criteria. You will use the Customers-Orders data-aware application from the previous tutorial, which allows end-users to view and edit customer details (see Tutorial 2 - Relations (One to Many)). For detailed information on querying data with XPO, see the concepts in the Querying a Data Store section.

Building Criteria

XPO provides multiple options for building simple filter criteria. For instance, the same criteria that represents a logical expression (Age > 30 for example) can be represented using two different notations, as shown in the code examples below.

  • Create a BinaryOperator. To do this, pass the appropriate operands as parameters to the operator’s constructor.

    
    using DevExpress.Data.Filtering;
    // ...
    private void Form1_Load(object sender, EventArgs e) {
        CriteriaOperator criteria = new BinaryOperator("Age", 30, BinaryOperatorType.Greater);
        // ...
    }
    
  • Criteria can also be represented as a human-readable string. This string must then be parsed using the static CriteriaOperator.Parse method.

    
    CriteriaOperator criteria = CriteriaOperator.Parse("Age > 30");
    

Thus, it is up to you to decide which method to use to build criteria. XPO delivers various criteria operators that allow logical expressions of considerable complexity to be constructed with ease.

Each temporary data store (XPCollection or XPView) has two properties that are used to filter objects on the data store and client sides:

  • Criteria - specifies the criteria used to filter objects on the data store side.
  • Filter - specifies the criteria used to filter objects on the client side.

Server-Side Criteria

To retrieve only those customers who are older than 30, assign the criteria to the XPCollection’s XPBaseCollection.Criteria property.


private void Form1_Load(object sender, EventArgs e) {
   // ...
   xpCollection1.Criteria = criteria;
}

You can also pass the criteria to the collection’s constructor instead of setting the XPBaseCollection.Criteria property, to create a collection with an initial set of criteria. For more information and examples, see How to: Build Complex Criteria.

At design time, you can use a convenient editor that allows you to visually construct a criteria expression. To invoke this editor, focus the XpCollection component and click the ellipsis button located to the right of Criteria in the Properties window.

Tutorial3-1

Client-Side Filter

Next, filter the retrieved objects (records) to display only those customers who are older than 30 and younger than 40. For this purpose, create a new criteria expression and assign it to the collection’s XPBaseCollection.Filter property.


private void Form1_Load(object sender, EventArgs e) {
    CriteriaOperator filter = CriteriaOperator.Parse("Age > 30 AND Age < 40");
    xpCollection1.Filter = filter;
    // ...
}

Note that because you are now using client side filtering, the initial ‘complete’ set of data can immediately be restored by setting the XPBaseCollection.Filter property to null (Nothing in Visual Basic), without any database access.

Results

Now that you have learned how to build and apply filter criteria, and how to filter persistent objects on the data store and client sides, continue to the next tutorial, A Windows Forms Application for Data Editing.

See Also