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

Filtering Basics

  • 3 minutes to read

Querying a data store allows you to retrieve data that matches specific criteria and store it within a temporary data store (e.g. XPCollection) for further review and evaluation. Data that is stored in a temporary data store can also be filtered.

In both instances, a filter criteria must be applied. The criteria represents the logical expression that is used as a data filter. Each temporary data store has two properties that are used to filter objects on the data store and client sides. These properties are listed and described below:

The following example demonstrates how to define a simple criteria to query the data store for those customers who are older than 40.

using DevExpress.Xpo;
using DevExpress.Data.Filtering;

class Customer : XPObject {
    public string Name {
        get { return fName; }
        set { SetPropertyValue(nameof(Name), ref fName, value); }
    }
    string fName;

    public int Age {
        get { return fAge; }
        set { SetPropertyValue(nameof(Age), ref fAge, value); }
    }
    int fAge;

}

...
// Specify the filter criteria.
BinaryOperator filterCriteria = new BinaryOperator(nameof(Customer.Age), 40, BinaryOperatorType.Greater);

// Retrieve only those customers who are older than 40.
XPCollection collection = new XPCollection(typeof(Customer), filterCriteria);

The image below shows the result:

QueryingFiltering1

The retrieved collection of persistent objects can also be filtered. Use its XPBaseCollection.Filter property to apply the desired filter. In the example below, the collection is filtered to return only those customers who are older than 42 but younger than 50.

collection.Filter = new BetweenOperator(nameof(Customer.Age), 42, 50);

The image below shows the result:

QueryingFiltering2

 

Note

Unsaved objects are not loaded into an XPCollection.

XPO queries the database and retrieves values. Then it instantiates persistent objects from the query result and adds these objects to the XPCollection. Before an object is instantiated, XPO searches for it in the cache by its Oid. If the object is found, its properties are refreshed and it is then added to the collection. Unsaved objects don’t have corresponding records in the database and therefore they don’t fall into the query result and thus aren’t added to the collection. This is the expected result. Objects must be saved to be included in the query results.