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 BasicImports 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