[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Collapse]eXpress Persistent Objects
 [Expand]Product Information
 [Expand]Getting Started
 [Expand]Feature Center
 [Expand]Design-Time Features
   How to: Access Data in SQL Query Results
   How to: Bind an XPCollection to a LookUp
   How to: Bind an XPCollection to Standard Controls
   How to: Bind an XPCollection to the DataGrid
   How to: Bind an XPCollection to the Grid
   How to: Build Complex Criteria
   How to: Build Simple Criteria
   How to: Change Inheritance Mapping
   How to: Connect to a SQL Server
   How to: Connect to SQL Azure
   How to: Connect XPO to a Database Other than MS SQL or MS Access
   How to: Control Automatic Saving of Objects when Editing in a Grid
   How to: Create an Aggregated Object
   How to: Delay Property Loading
   How to: Filter By DateTime Values
   How to: Filter Persistent Objects by Type
   How to: Handle Persistent Object Initialization
   How to: Implement a Custom Criteria Language Function Operator
   How to: Implement a Full-Text Search
   How to: Implement Custom Functions and Criteria in LINQ to XPO
   How to: Implement Many-to-Many Relationships
   How to: Implement One-to-One Relationships
   How to: Link Classes Located in Different Assemblies
   How to: Make a Class or Property Non-Persistent
   How to: Make XPCollection Create Objects of Specific Type when They are Created via Bound Controls
   How to: Map to Custom Tables (Views) and Columns
   How to: Obtain a Collection of Persistent Objects by a Set of Their IDs
   How to: Obtain a Collection of Persistent Objects for Processing
   How to: Persist a BLOB Field
   How to: Prevent Changes in a Persistent Object from being Automatically Committed
   How to: Restore Deleted Objects
   How to: Use Pageable Collections
   How to: Use Read-Only Persistent Properties
   How to: Use the ContainsOperator for Objects in a Many-to-Many Relationship
   How to: Validate an Object
   How to: Work with Structures
 [Expand]Member Tables
 [Expand]API Reference
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

How to: Build Complex Criteria

This topic demonstrates advanced options for creating criteria when retrieving persistent objects using collections such as the XPCollection or filtering their contents. Please refer to the How to Build Simple Criteria topic for general information on building criteria in XPO.

Expanded Using Logical Groups

Simple criteria can be combined into logical groups to form complex criteria. For example, to retrieve all the contacts that do not live in Chicago, you can use the simple criteria as is shown in the following code example.

The code below represents an extended version of the previous code example and demonstrates how to retrieve all the contacts that do not live in Chicago, but do work for a company. Note that these two criteria are combined using the AND operator.

Expanded Filtering By Querying Collection Properties

The ContainsOperator allows you to build filter criteria based on the results of querying collection properties. The following code selects a set of Contact objects that contain an Address in their Locations collection property and the City property of the Address objects found should be equal to the value of the Owner.DefaultAddress.City property, i.e. the value of the person's DefaultAddress.City property.


To reference a parent object in an OperandProperty (the Owner property in the code example below), use the "^" symbol.

Expanded Using Aggregate Data

With AggregateOperand, you can calculate aggregate operations using collection properties and create criteria based on the aggregate values. Let us consider a simple example.

To retrieve all persons who have less than 100 dollars in their cumulative accounts, you can use the following code.


For a list of aggregate functions, see Aggregate.

An aggregate operation applies either to all objects within the collection or to the objects that match the explicitly specified criteria. For example, the following code selects all persons that have only one account with a zero amount.

Expanded Narrowing Results in Complex Inheritance Hierarchies

In a complex inheritance hierarchy, querying a data store for the objects whose type is used as the type of a base class for their descendants (such as the Person class for the Company class in the example above) results in objects of the base type and their descendants being retrieved. In order to avoid an ambiguous definition of the type of objects retrieved by the collection, you can explicitly specify the object type you want to appear in your collection as is shown in the following code example.

Expanded Querying from the Backend without Creating Objects

XPView can query information from the database and make it available in a number of "fields", which have to be defined for the view beforehand.

The code below creates an XPView with a calculated field:

Since the XPView's result set doesn't contain objects of the given type, the view's field from the code above can be accessed like this:

Expanded See Also

Is this topic helpful?​​​​​​​