Skip to content

LINQ AsyncEnumerableExtensions

ZZZ Projects edited this page Jan 24, 2016 · 1 revision

Async Task extension methods allow to perform operation on IEnumerable<Task<T>>.

Support:

  • OrderByCompletion
  • SelectResult
  • SelectResultByCompletion
// Using Z.Linq

public async Task<List<Customer>> MyAsyncTaskMethod(CancellationToken cancellationToken)
{
    int customerID = 4;
    
    // GET customer from concurrent web service
    IEnumerable<Task<List<Customer>>> task =  WebService.GetCustomers(4);
    
    // GET the customer list from the first web service completed
    var taskFirstCompleted = task.SelectResultByCompletion()
                                 .First();
                   
    // GET the five first customers which the predicate has completed
    var task = taskFirstCompleted.WhereAsync(c => MyAsyncPredicate(DB.IsCustomerActiveAsync(c)))
                                 .OrderByPredicateCompletion()
                                 .Take(5)
                                 .ToList();
    
    // ... synchronous code ...
    
    return task;
}