KB Article A2960
Visible to All Users

What is the best way to obtain a collection of persistent objects for processing?

Description:
The task is: retrieve objects by particular criteria from the database, process them and save them. I don't need to display objects in data bound visual controls. It's a kind of background data processing, where the low memory footprint and performance are critical. Is there a better way to get a list of the objects other than loading them into an XPCollection?

Answer:
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. We advise you to use the Session.GetObjects method in your task. Below is some sample code.

C#
using DevExpress.Xpo; using DevExpress.Xpo.DB; using DevExpress.Xpo.Metadata; using DevExpress.Data.Filtering; using System.Collections; void ProcessDiscontinued() { XPClassInfo productClass; CriteriaOperator criteria; SortingCollection sortProps; using(UnitOfWork uow = new UnitOfWork()) { // Obtain the persistent object class info required by the GetObjects method productClass = uow.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", SortingDirection.Ascending)); // Call GetObjects ICollection products = uow.GetObjects(productClass, criteria, sortProps, int.MaxValue, false, 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 changes if any //if(uow.InTransaction) // uow.CommitChanges(); } }
Visual Basic
Sub ProcessDiscontinued() Dim products As ICollection Dim productClass As XPClassInfo Dim criteria As CriteriaOperator Dim sortProps As SortingCollection Dim session As New UnitOfWork ' Obtain the persistent object class info required by the GetObjects method productClass = session.GetClassInfo(GetType(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(Nothing) sortProps.Add(New SortProperty("Price", SortingDirection.Ascending)) ' Call GetObjects products = session.GetObjects(productClass, criteria, sortProps, Integer.MaxValue, False, True) ' Do processing Dim product As Product For Each 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) Next ' Save changes if any 'If session.InTransaction Then ' session.CommitChanges() 'End If session.Dispose() End Sub

See Also:
How to obtain a collection of persistent objects by a set of their IDs

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.