This project provides a set of extension methods that enhance the LINQ (Language Integrated Query) functionality in C# by adding support for asynchronous operations. It includes extensions for common LINQ operations such as Select, SelectMany, and Where, allowing developers to work with asynchronous sources and transformations.
To use these extensions, install the nuget package for your C# project and ensure that the appropriate namespaces (System.Linq, etc.) are referenced. Make sure that you have the necessary dependencies and target framework version set correctly.
dotnet add package LinqExtensionThe ForeachExtensions class contains extension methods for asynchronous iteration over collections using foreach loops. It includes the following methods:
- ForEach: Iterates over an enumerable and executes an action synchronously.
- ForEachAsync: Iterates over an enumerable and executes an asynchronous action.
- ForEach (for IAsyncEnumerable): Iterates over an async enumerable and executes an action.
The ListExtensions class provides an extension method for converting an IAsyncEnumerable or Task to a List asynchronously. It includes the following method:
- ToListAsync: Converts an asynchronous enumerable to a list.
The SelectExtensions class adds async projection capabilities to the Select operation. It offers various overloads to handle synchronous and asynchronous transform functions. The methods included are:
- Select (for Task): Projects elements asynchronously using a synchronous transform function.
- Select (for IAsyncEnumerable): Projects elements asynchronously using a synchronous transform function.
- SelectAsync (for IEnumerable): Projects elements asynchronously using an asynchronous transform function.
- SelectAsync (for Task): Projects elements asynchronously using an asynchronous transform function.
- SelectAsync (for IAsyncEnumerable): Projects elements asynchronously using an asynchronous transform function.
The SelectManyExtensions class extends the SelectMany operation with async support. It allows flattening and transforming collections asynchronously using synchronous and asynchronous transform functions. The methods provided are:
- SelectMany (for Task): Flattens and transforms elements asynchronously using a synchronous transform function.
- SelectManyAsync (for Task): Flattens and transforms elements asynchronously using an asynchronous transform function.
- SelectManyAsync (for IEnumerable): Flattens and transforms elements asynchronously using an asynchronous transform function.
- SelectManyAsync (for IAsyncEnumerable): Flattens and transforms elements asynchronously using an asynchronous transform function.
- SelectMany (for IAsyncEnumerable): Flattens and transforms elements asynchronously using a synchronous transform function.
The WhereExtensions class adds async filtering capabilities to the Where operation. It allows filtering elements from asynchronous sources based on synchronous and asynchronous conditions. The methods included are:
- Where (for Task): Filters elements asynchronously using a synchronous condition.
- Where (for IAsyncEnumerable): Filters elements asynchronously using a synchronous condition.
- WhereAsync (for IEnumerable): Filters elements asynchronously using an asynchronous condition.
- WhereAsync (for Task): Filters elements asynchronously using an asynchronous condition.
- WhereAsync (for IAsyncEnumerable): Filters elements asynchronously using an asynchronous condition.
Feel free to explore and leverage these extensions to incorporate async operations into your LINQ queries.