Skip to main content

Filtering

  • 4 minutes to read

XPO allows you to:

  • filter data items in a data store prior to retrieving data,
  • filter the already retrieved persistent objects on the client side.

Filter Data on the Server Side

Using LINQ to XPO

The following example demonstrates how to use LINQ to XPO to query the data store for those customers who are older than 40.

using System.Linq;
using System.Linq.Expressions;
using DevExpress.Xpo;

// ...

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;

}

// ...

using (var uow = new UnitOfWork()){
    // Select customers older than 40
    var list = uow.Query<Customer>().Where(c => c.Age > 40);

    foreach (Customer cust in list)
        Console.WriteLine(string.Format("{0}\t{1}", cust.Name,
            cust.Age));
}

Using the Criteria Property

Use the Criteria property (for example, XPBaseCollection.Criteria, XPView.Criteria, or XpoDataSource.Criteria) to specify the criteria used to filter objects on the data store side.

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

Filter Data on the Client Side

You can filter the collections of the retrieved persistent objects. Use the Filter (for example, XPBaseCollection.Filter, XPView.Filter, or XPDataView.Filter) property to specify the criteria used to filter objects on the client side.

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.

Creating Criteria

XPO supports multiple ways for writing criteria. The common ways of doing this are: creating criteria in code as CriteriaOperator objects, and using the static CriteriaOperator.Parse method.

Task-Based Help

Member Table

Online Knowledge Base