KB Article A967
Visible to All Users

How to create an empty XPCollection of a specified type

Description:
I want to select several objects by a complex criteria which cannot be executed by the database server (there are non-persistent properties and type castings). I can retrieve them and put them into an array and then show this array within a grid, but by doing this I lose a lot of the XPCollection's functionality. I can create a collection with a criteria which returns no records and fill it manually, but this solution requires unnecessary round trips to the server and query executions. Is there a more appropriate way to create an empty XPCollection?

Answer:
If you are using XPO 6.1 and later, you just need to set the XPCollection.LoadingEnabled property to false.
In XPO 1.x you should create a custom descendant of the XPCollection class to implement this functionality. You can override the loading methods of the XPCollection and disable the objects loading. Please note that this solution is applicable to XPO 1.x only.

C#
using DevExpress.Xpo; public class EmptyXPCollection : XPCollection { public EmptyXPCollection(System.Type objType): base(objType) {} public EmptyXPCollection(Session session, System.Type objType): base(session, objType) {} protected override void LoadContent() {} public override void Reload() {} }
Visual Basic
Imports DevExpress.Xpo Public Class EmptyXPCollection Inherits XPCollection Public Sub New(ByVal objType As Type) MyBase.New(objType) End Sub Public Sub New(ByVal session As Session, ByVal objType As Type) MyBase.New(session, objType) End Sub Protected Overrides Sub LoadContent() End Sub Public Overrides Sub Reload() End Sub End Class

This collection won't then automatically load its contents and you can fill it yourself by adding objects via the Add or AddRange method.
See Also:
How to define a one-to-many relation without introducing new properties on the "one" side
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.