xpo-2036-query-and-shape-data-filtering.md
XPO allows you to:
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));
}
Imports System.Linq;
Imports System.Linq.Expressions;
Imports DevExpress.Xpo
'...
Class Customer
Inherits XPObject
Public Property Name() As String
Get
Return fName
End Get
Set(ByVal value as String)
SetPropertyValue(NameOf(Name), fName, value)
End Set
End Property
Private fName As String
Public Property Age() As Integer
Get
Return fAge
End Get
Set(ByVal value as Integer)
SetPropertyValue(NameOf(Age), fAge, value)
End Set
End Property
Private fAge As Integer
End Class
'...
Using uow = New UnitOfWork()
' Select customers older than 40
Dim list = uow.Query(Of Customer)().Where(Function(c) c.Age > 40)
For Each cust As Customer In list
Console.WriteLine(customer.Name & Constants.vbTab & _
customer.Age)
Next
End Using
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);
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
Class Customer
Inherits XPObject
Public Property Name() As String
Get
Return fName
End Get
Set(ByVal value as String)
SetPropertyValue(NameOf(Name), fName, value)
End Set
End Property
Private fName As String
Public Property Age() As Integer
Get
Return fAge
End Get
Set(ByVal value as Integer)
SetPropertyValue(NameOf(Age), fAge, value)
End Set
End Property
Private fAge As Integer
End Class
...
' Specify the filter criteria.
Dim filterCriteria As BinaryOperator = New BinaryOperator(NameOf(Customer.Age), 40, BinaryOperatorType.Greater)
' Retrieve only those customers who are older than 40.
Dim collection As XPCollection = New XPCollection(GetType(Customer), filterCriteria)
The image below shows the result:
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);
collection.Filter = New BetweenOperator(NameOf(Customer.Age), 42, 50)
The image below shows the result:
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.
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.