The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.
.NET Framework 4.5.2+
.NET Framework 4.5.2+
.NET Standard 2.0+

How to: Obtain a Collection of Persistent Objects for Processing

  • 2 min to read

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 the Session.GetObjects method as shown in the example below:


void ProcessDiscontinued() {
    ICollection products;
    DevExpress.Xpo.Metadata.XPClassInfo productClass;
    DevExpress.Data.Filtering.CriteriaOperator criteria;
    DevExpress.Xpo.SortingCollection sortProps;
    DevExpress.Xpo.Session session;
    DevExpress.Xpo.Generators.CollectionCriteriaPatcher patcher;

    session = new Session();
    session.ConnectionString = XpoDefault.ConnectionString;

    // Obtain the persistent object class info required by the GetObjects method
    productClass = session.GetClassInfo(typeof(Product));
    // Create criteria to get objects
    criteria = new BinaryOperator("Discontinued", true);
    // Create a sort list if objects must be processed in a specific order
    sortProps = new SortingCollection(null);
    sortProps.Add(new SortProperty("Price", DevExpress.Xpo.DB.SortingDirection.Ascending));

    // Create criteria patcher to filter out the objects marked as "deleted" 
    // and to support loading of inherited objects of a given base persistent class
    patcher = new DevExpress.Xpo.Generators.CollectionCriteriaPatcher(false, session.TypesManager);

    // Call GetObjects
    products = session.GetObjects(productClass, criteria, sortProps, 0, patcher, true);

    // Do processing
    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.Discontinued);
    }

    // Save if necessary
    //session.Save(products)

    session.Dispose();
}