Skip to main content
.NET 8.0+

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

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

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

Namespace: DevExpress.ExpressApp

Assembly: DevExpress.ExpressApp.v24.2.dll

NuGet Package: DevExpress.ExpressApp

#Declaration

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

#Parameters

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
T

The type of elements in the specified collection.

#Returns

Type Description
Task<List<T>>

A Task that returns a list of objects.

#Remarks

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() {
        base.OnActivated();
        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);
            ViewCurrentObject.Contacts.AddRange(contactsList);
        }
        finally {
            if (handle != null) {
                application.StopOverlayForm(handle);
            }
        }
    }
}

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