Skip to content

zzzprojects/LINQ-Async

Repository files navigation

What's LINQ-Async?

LINQ-Async allows you to chain async task and orders async predicate with fluent API.

Features

Sponsors

ZZZ Projects owns and maintains LINQ-Async as part of our mission to add value to the .NET community

Through Entity Framework Extensions and Dapper Plus, we actively sponsor and help key open-source libraries grow.

Entity Framework Extensions

Dapper Plus

Download

nuget nuget

PM> NuGet\Install-Package Z.EntityFramework.Plus.EFCore
> dotnet add package Z.EntityFramework.Plus.EFCore

LINQ Async Extensions

Problem

You want to use LINQ methods asynchronously.

Solution

All LINQ extension methods and overloads are supported. You can easily create any asynchronous task.

// Using Z.Linq

public Task<IEnumerable<Customer>> MyAsyncMethod(CancellationToken cancellationToken)
{
    List<Customer> customers = DB.GetCustomers();
    var task = list.WhereAsync(c => /* long predicate */, cancellationToken);

    // ... synchronous code ...
    
    return task;
}

LINQ Async Predicate Extensions

Problem

You want to resolve a predicate asynchronously and start all predicates concurrently and/or order them by completion.

Solution

All LINQ extension methods and overloads using a predicate are supported. You can easily use an asynchronously predicate and choose how the predicate will be resolved:

  • OrderByPredicateCompletion(bool)
  • StatePredicateConcurrently(bool)

Support:

  • Deferred
    • SkipWhile
    • Where
  • Immediate
    • All
    • Any
    • Count
    • First
    • FirstOrDefault
    • LongCount
    • Single
    • SingleOrDefault
// Using Z.Linq

// Change global default value
LinqAsyncManager.DefautlValue.OrderByPredicateCompletion = false;
LinqAsyncManager.DefaultValue.StartPredicateConcurrently = false;

public Task<IEnumerable<Customer>> MyAsyncTaskMethod(CancellationToken cancellationToken)
{
    List<Customer> customers = DB.GetCustomers();
    
    // GET all customers by predicate completion
    var task = list.WhereAsync(c => MyAsyncPredicate(DB.IsCustomerActiveAsync(c)))
                   .OrderByPredicateCompletion();

    // ... synchronous code ...
    
    return task;
}

Learn more

LINQ Async Task Extensions

Problem

You want to chain LINQ methods with Task<IEnumerable<T>>.

Solution

All LINQ extensions methods and overloads are supported. You can easily chain multiples LINQ methods before awaiting your final task.

Support:

  • Array
  • Enumerable
  • List

Other types must use "AsEnumerable()" method to allow to chain LINQ methods.

// Using Z.Linq

public async Task<List<Customer>> MyAsyncTaskMethod(CancellationToken cancellationToken)
{
    // GET the five first customers which the predicate has completed
    var task = list.WhereAsync(c => MyAsyncPredicate(DB.IsCustomerActiveAsync(c)))
                         .OrderByPredicateCompletion()
                         .Take(5)
                         .ToList();


    // ... synchronous code ...
    
    return task;
}

Learn more

LINQ Async Enumerable Task Extensions

Problem

You want to use LINQ methods with enumerable tasks and order them by completion.

Solution

Support:

  • OrderByCompletion
  • SelectResult
// Using Z.Linq

public async Task<List<Customer>> MyAsyncTaskMethod(CancellationToken cancellationToken)
{
    // GET customer from concurrent web service
    IEnumerable<Task<List<Customer>>> task =  WebService.GetCustomers();
    
    // GET the customer list from the first web service completed
    var taskFirstCompleted = task.SelectResultByCompletion()
                                 .SelectResult()
								 .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;
}

Learn more

More Projects

To view all our free and paid projects, visit our website ZZZ Projects.

About

C# LINQ Async extension methods library for async/await task.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

  •  

Languages