Skip to main content
.NET Framework 4.5.2+

XPObjectSpace.ToListAsync<T>(Object, CancellationToken) Method

Asynchronously enumerates all elements in a collection and saves them to a list.

Namespace: DevExpress.ExpressApp.Xpo

Assembly: DevExpress.ExpressApp.Xpo.v23.1.dll

NuGet Package: DevExpress.ExpressApp.Xpo


public Task<List<T>> ToListAsync<T>(
    object collection,
    CancellationToken cancellationToken = default(CancellationToken)


Name Type Description
collection Object

A collection to be enumerated.

Optional Parameters

Name Type Default Description
cancellationToken CancellationToken null

A CancellationToken object that delivers a cancellation notice to the running operation.

Type Parameters

Name Description

The type of elements in the specified collection.


Type Description

A Task that returns a list of objects.


Use the GetObjectsQuery<T>(Boolean) method to get the XPQuery<T> object to pass it as the ToListAsync<T>(Object, CancellationToken)‘s collection parameter. You can also create this object manually.

The following code demonstrates how you can use this method in a WinForms-specific View Controller to add contacts from the Development Department to the task’s Contacts collection.

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Win;
using DevExpress.ExpressApp.Xpo;
using DevExpress.XtraSplashScreen;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
// ...
public class AsyncTaskContactsController : ObjectViewController<DetailView, DemoTask> {
    public AsyncTaskContactsController() : base() {
        SimpleAction assignToDepartmentAction = new SimpleAction(this, "Assign to the Dev.Departmant", "Edit");
        assignToDepartmentAction.SelectionDependencyType = SelectionDependencyType.RequireSingleObject;
        assignToDepartmentAction.Execute += assignToDepartmentAction_Execute;
    async private void assignToDepartmentAction_Execute(object sender, SimpleActionExecuteEventArgs e) {
        IOverlaySplashScreenHandle handle = null;
        Control control = Frame.Template as Control;
        WinApplication application = Application as WinApplication;
        CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
        var contacts = from c in ObjectSpace.GetObjectsQuery<Contact>()
                        where c.Department.Title == "Development Department"
                        select c;
        IList<Contact> contactsList = null;
        try {
            if (control != null && control.IsHandleCreated) {
                handle = application.StartOverlayForm(control);
            contactsList = await ((XPObjectSpace)ObjectSpace).ToListAsync<Contact>(contacts, cancellationTokenSource.Token);
        finally {
            if (handle != null) {

In the current example, the cancellationToken parameter is used for demonstration purposes. You can skip it or use to cancel an asynchronous operation as shown in the CancellationToken topic.

See Also