Back to Devexpress

How to: Obtain a Collection of Persistent Objects for Processing

xpo-3236-examples-how-to-obtain-a-collection-of-persistent-objects-for-processing.md

latest1.9 KB
Original Source

How to: Obtain a Collection of Persistent Objects for Processing

  • Mar 19, 2020

The XPCollection is a component with a lot of features. It implements the IBindingList and ITypedList interfaces, supports on-the-fly filtering and sorting and it can be bound to visual controls. Obviously, you don’t need this functionality for background data processing. It may make sense to load objects into a simple array rather than to use the XPCollection. Use LINQ to XPO as shown in the example below:

csharp
using System;
using System.Collections.Generic;
using System.Linq;
using DevExpress.Xpo;
// ...
void ProcessDiscontinued(Session session) {
    IEnumerable<Product> products = session.Query<Product>()
        .Where(p => p.IsDiscontinued)
        .OrderBy(p => p.Price);
    foreach(Product product in products) {
        // Do some action with the product object.
        Console.WriteLine("{0,-25} {1,8:c} discontinued: {2}", product.Name, product.Price, 
          product.IsDiscontinued);
    }
}
vb
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports DevExpress.Xpo
' ...
Private Sub ProcessDiscontinued(ByVal session As Session)
    Dim products As IEnumerable(Of Product) = session.Query(Of Product)() _
        .Where(Function(p) p.IsDiscontinued) _
        .OrderBy(Function(p) p.Price)
    For Each product As Product In products
        ' Do some action with the product object.
        Console.WriteLine("{0,-25} {1,8:c} discontinued: {2}", product.Name, product.Price, product.IsDiscontinued)
    Next product
End Sub