Xpandables.AsyncPaged.Linq 10.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Xpandables.AsyncPaged.Linq --version 10.0.1
                    
NuGet\Install-Package Xpandables.AsyncPaged.Linq -Version 10.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Xpandables.AsyncPaged.Linq" Version="10.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Xpandables.AsyncPaged.Linq" Version="10.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Xpandables.AsyncPaged.Linq" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Xpandables.AsyncPaged.Linq --version 10.0.1
                    
#r "nuget: Xpandables.AsyncPaged.Linq, 10.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Xpandables.AsyncPaged.Linq@10.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Xpandables.AsyncPaged.Linq&version=10.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Xpandables.AsyncPaged.Linq&version=10.0.1
                    
Install as a Cake Tool

System.AsyncPaged.Linq

NuGet NuGet Downloads .NET License

Comprehensive LINQ operators for IAsyncPagedEnumerable<T> with projection, filtering, aggregation, and materialization support.

Overview

System.AsyncPaged.Linq extends IAsyncPagedEnumerable<T> with a complete suite of LINQ operators specifically designed for async pagination scenarios. All operators preserve pagination metadata throughout the query chain.

Built for .NET 10 with AOT compatibility.

Features

Projection (ProjectionExtensions)

  • SelectPaged — Synchronous projection with optional index
  • SelectPagedAsync — Async projection
  • SelectManyPaged — Flatten nested enumerables
  • SelectManyPagedAsync — Async flatten

Set Operations (SetExtensions)

  • DistinctPaged — Remove duplicates
  • DistinctByPaged — Remove duplicates by key
  • UnionPaged — Combine sequences
  • IntersectPaged — Common elements
  • ExceptPaged — Exclude elements

Ordering (OrderingExtensions)

  • OrderByPaged / OrderByPagedDescending — Primary ordering
  • ThenByPaged / ThenByPagedDescending — Secondary ordering
  • ReversePaged — Reverse sequence order

Grouping (GroupingExtensions)

  • GroupByPaged — Partition by key with optional element and result selectors

Joining (JoinExtensions)

  • JoinPaged — Inner join sequences
  • GroupJoinPaged — Group join for one-to-many relationships

Windowing & Partitioning (WindowingExtensions)

  • TakePaged / TakeLastPaged — Take elements from start/end
  • SkipPaged / SkipLastPaged — Skip elements from start/end
  • TakeWhilePaged / SkipWhilePaged — Conditional take/skip
  • ChunkPaged — Split into fixed-size chunks
  • WindowPaged — Sliding window of elements
  • WindowedSumPaged / WindowedAveragePaged — Windowed aggregates
  • WindowedMinPaged / WindowedMaxPaged — Windowed min/max
  • PairwisePaged — Process consecutive pairs
  • ScanPaged — Running accumulator

Transformation (TransformationExtensions)

  • WherePaged — Filter with predicate
  • CastPaged — Type cast elements
  • OfTypePaged — Filter by type

Materialization (MaterializationExtensions)

  • ToListPagedAsync — Materialize to list
  • ToArrayPagedAsync — Materialize to array
  • MaterializeAsync — Materialize with custom collection
  • PrecomputePaginationAsync — Compute pagination upfront

Pagination (PaginationExtensions)

  • ToAsyncPagedEnumerable — Convert IQueryable<T> or IAsyncEnumerable<T>

Installation

dotnet add package Xpandables.AsyncPaged.Linq

Dependency: Xpandables.AsyncPaged (installed automatically)

Quick Start

Basic LINQ Operations

using System.Linq;

IAsyncPagedEnumerable<Product> products = GetProductsAsync();

// Projection
var productNames = products.SelectPaged(p => p.Name);

// Filtering
var activeProducts = products.WherePaged(p => p.IsActive);

// Ordering
var sorted = products.OrderByPaged(p => p.Name);

// Enumerate
await foreach (var product in activeProducts)
{
    Console.WriteLine(product.Name);
}

// Pagination metadata
var pagination = await products.GetPaginationAsync();
Console.WriteLine($"Page {pagination.CurrentPage} of {pagination.TotalPages}");

Async Projection

var enrichedProducts = products.SelectPagedAsync(async product =>
{
    var reviews = await _reviewService.GetReviewsAsync(product.Id);
    return new { product.Name, ReviewCount = reviews.Count };
});

Windowed Analysis

// 7-day moving average
var movingAverages = products
    .OrderByPaged(p => p.CreatedAt)
    .WindowedAveragePaged(windowSize: 7, p => p.Price);

// Pairwise comparison
var priceChanges = products
    .PairwisePaged((prev, curr) => curr.Price - prev.Price);

Materialization

// Materialize to list with pagination preserved
var list = await products.ToListPagedAsync();

// Precompute pagination for multiple enumerations
var precomputed = await products.PrecomputePaginationAsync();

License

Apache License 2.0 .Where(p ⇒ p.IsActive) .ToAsyncPagedEnumerable();

// Apply filters
if (!string.IsNullOrWhiteSpace(searchTerm))
    products = products.WherePaged(p => p.Name.Contains(searchTerm));

if (minPrice.HasValue)
    products = products.WherePaged(p => p.Price >= minPrice.Value);

if (maxPrice.HasValue)
    products = products.WherePaged(p => p.Price <= maxPrice.Value);

// Project to DTO
var dtos = products.SelectPaged(p => new ProductDto
{
    Id = p.Id,
    Name = p.Name,
    Price = p.Price,
    Category = p.Category.Name
});

// Sort and return
return dtos.OrderByPagedDescending(p => p.Price);

}

// Usage var results = await SearchAsync("laptop", minPrice: 500, maxPrice: 2000, pageSize: 10); await foreach (var product in results) { Console.WriteLine($"{product.Name} - ${product.Price}"); }

var pagination = await results.GetPaginationAsync();


### 🎯 Data Enrichment

```csharp
// Enrich items from paged source with additional async data
var enrichedUsers = _context.Users
    .Where(u => u.IsActive)
    .ToAsyncPagedEnumerable()
    .SelectPagedAsync(async user =>
    {
        var orderCount = await _context.Orders
            .Where(o => o.UserId == user.Id)
            .CountAsync();

        var totalSpent = await _context.Orders
            .Where(o => o.UserId == user.Id)
            .SumAsync(o => o.Total);

        return new UserSummary
        {
            Id = user.Id,
            Name = user.Name,
            Email = user.Email,
            OrderCount = orderCount,
            TotalSpent = totalSpent
        };
    });

await foreach (var summary in enrichedUsers)
{
    Console.WriteLine($"{summary.Name}: {summary.OrderCount} orders, ${summary.TotalSpent}");
}

💾 Materialization for Multiple Enumerations

// When you need to enumerate multiple times, materialize to avoid re-querying
var paged = _context.Products.Where(p => p.IsActive).ToAsyncPagedEnumerable();

// Materialize with all items in memory
var materialized = await paged.MaterializeAsync();

// Now you can enumerate multiple times efficiently
int count = await materialized.CountAsync();
var maxPrice = await materialized.MaxAsync(p => p.Price);
var minPrice = await materialized.MinAsync(p => p.Price);

// Enumerate items
await foreach (var product in materialized)
{
    Console.WriteLine(product.Name);
}

📦 Flattening Nested Collections

// Flatten orders and their items while preserving pagination
var orderItems = _context.Orders
    .ToAsyncPagedEnumerable()
    .SelectManyPagedAsync(order => 
        _context.OrderItems
            .Where(oi => oi.OrderId == order.Id)
            .ToAsyncEnumerable(),
        (order, item) => new
        {
            OrderId = order.Id,
            ItemId = item.ProductId,
            Quantity = item.Quantity,
            Price = item.Price
        });

await foreach (var item in orderItems)
{
    Console.WriteLine($"Order {item.OrderId}: {item.Quantity}x at ${item.Price}");
}

🎯 Advanced Examples

📈 Aggregation Pipeline

// Complex aggregation with grouping
var salesByCategory = _context.Sales
    .Where(s => s.Date.Year == 2024)
    .ToAsyncPagedEnumerable()
    .GroupBy(s => s.Category);

var report = new List<CategoryReport>();

foreach (var group in salesByCategory)
{
    var pagedGroup = group.ToAsyncPagedEnumerable();
    var total = await pagedGroup.SumAsync(s => s.Amount);
    var count = await pagedGroup.CountAsync();
    var avg = await pagedGroup.AverageAsync(s => s.Amount);

    report.Add(new CategoryReport
    {
        Category = group.Key,
        TotalSales = total,
        SalesCount = count,
        AverageSale = avg
    });
}

foreach (var category in report)
{
    Console.WriteLine($"{category.Category}: ${category.TotalSales} ({category.SalesCount} sales)");
}

🔄 Async Transformation with Cancellation

// Support cancellation in async projections
var processed = products.SelectPagedAsync(
    async (product, cancellationToken) =>
    {
        // Long-running operation with cancellation support
        var enhanced = await EnhanceProductAsync(product, cancellationToken);
        return enhanced;
    });

// Enumerate with cancellation
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
await foreach (var item in processed.WithCancellation(cts.Token))
{
    Console.WriteLine(item.Name);
}

🪟 Windowing & Chunking Operations

// Skip first 20, take next 20 items
var paged = products
    .SkipPaged(20)
    .TakePaged(20);

await foreach (var product in paged)
{
    Console.WriteLine(product.Name);
}

// Chunk into batches
var batches = products.ChunkPaged(size: 50);
await foreach (var batch in batches)
{
    Console.WriteLine($"Processing batch of {batch.Length} items");
    // Process batch
}

// Sliding window analysis
var windows = timeSeries
    .WindowPaged(windowSize: 7);

await foreach (var window in windows)
{
    var avg = window.Average();
    Console.WriteLine($"7-day window average: {avg}");
}

🎯 Finding Specific Elements

// Get first item matching criteria
var firstExpensive = await products
    .WherePaged(p => p.Price > 1000)
    .FirstPagedAsync();

// Get single item or throw if not found
var singleProduct = await products
    .WherePaged(p => p.Id == productId)
    .SinglePagedAsync();

// Get element at specific index
var fifthProduct = await products.ElementAtPagedAsync(4);

✅ Best Practices

✅ Do

  • Chain operators fluently — Take advantage of method chaining for readable queries
  • Use *PagedAsync methods for terminal operations — They provide proper async/await semantics
  • Apply filtering early — Filter with WherePaged before projection to reduce data processed
  • Materialize when needed — Use MaterializeAsync() for small datasets requiring multiple enumerations
  • Precompute pagination when appropriate — Use PrecomputePaginationAsync() to eagerly compute pagination metadata once
  • Support cancellation — Pass CancellationToken through async operations
  • Preserve pagination context — Let operators maintain pagination metadata automatically
  • Use analytical operations efficiently — Leverage WindowPaged, PairwisePaged, ScanPaged for time-series and trend analysis
  • Use MinBy/MaxBy — Find items with extreme key values without manual comparison

❌ Don't

  • Materialize large datasets unnecessarily — Avoid loading entire result sets into memory with MaterializeAsync()
  • Mix filtering patterns inconsistently — Use WherePaged + SelectPaged for clear separation of concerns
  • Forget pagination metadata — Always call GetPaginationAsync() when displaying page info
  • Block on async operations — Never use .Result or .Wait() on paged operations
  • Ignore cancellation tokens — Support graceful cancellation in long-running queries
  • Compute pagination multiple times — If accessing pagination repeatedly, use PrecomputePaginationAsync() first

📖 API Reference

🔄 Projection Extensions

// Synchronous projection
IAsyncPagedEnumerable<TResult> SelectPaged<TResult>(
    Func<TSource, TResult> selector);

// Asynchronous projection (without cancellation)
IAsyncPagedEnumerable<TResult> SelectPagedAsync<TResult>(
    Func<TSource, ValueTask<TResult>> selectorAsync);

// Asynchronous projection (with cancellation)
IAsyncPagedEnumerable<TResult> SelectPagedAsync<TResult>(
    Func<TSource, CancellationToken, ValueTask<TResult>> selectorAsync);

🔍 Filtering & Partitioning Extensions

// Synchronous predicate filtering
IAsyncPagedEnumerable<TSource> WherePaged(Func<TSource, bool> predicate);
IAsyncPagedEnumerable<TSource> WherePaged(Func<TSource, int, bool> predicate);

// Partitioning
IAsyncPagedEnumerable<TSource> TakePaged(int count);
IAsyncPagedEnumerable<TSource> SkipPaged(int count);
IAsyncPagedEnumerable<TSource> TakeLastPaged(int count);
IAsyncPagedEnumerable<TSource> SkipLastPaged(int count);

// Conditional partitioning
IAsyncPagedEnumerable<TSource> TakeWhilePaged(Func<TSource, bool> predicate);
IAsyncPagedEnumerable<TSource> TakeWhilePaged(Func<TSource, int, bool> predicate);
IAsyncPagedEnumerable<TSource> SkipWhilePaged(Func<TSource, bool> predicate);
IAsyncPagedEnumerable<TSource> SkipWhilePaged(Func<TSource, int, bool> predicate);

// Chunking & Distinctness
IAsyncPagedEnumerable<TSource[]> ChunkPaged(int size);
IAsyncPagedEnumerable<TSource> DistinctPaged();
IAsyncPagedEnumerable<TSource> DistinctPaged(IEqualityComparer<TSource>? comparer);
IAsyncPagedEnumerable<TSource> DistinctByPaged<TKey>(Func<TSource, TKey> keySelector);
IAsyncPagedEnumerable<TSource> DistinctByPaged<TKey>(Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer);

💾 Materialization Extensions

// Fully materialize paged enumerable
ValueTask<IAsyncPagedEnumerable<T>> MaterializeAsync(
    this IAsyncPagedEnumerable<T> source,
    CancellationToken cancellationToken = default);

ValueTask<IAsyncPagedEnumerable<T>> MaterializeAsync(
    this IAsyncPagedEnumerable<T> source,
    int pageSize,
    int currentPage = 1,
    CancellationToken cancellationToken = default);

// Convert IAsyncEnumerable to materialized paged enumerable
ValueTask<IAsyncPagedEnumerable<T>> ToMaterializedAsyncPagedEnumerable<T>(
    this IAsyncEnumerable<T> source,
    CancellationToken cancellationToken = default);

ValueTask<IAsyncPagedEnumerable<T>> ToMaterializedAsyncPagedEnumerable<T>(
    this IAsyncEnumerable<T> source,
    int pageSize,
    int currentPage = 1,
    CancellationToken cancellationToken = default);

// Precompute pagination metadata (extension method on IAsyncPagedEnumerable<T>)
ValueTask<IAsyncPagedEnumerable<T>> PrecomputePaginationAsync(
    CancellationToken cancellationToken = default);

🔬 Analytical & Windowing Extensions

// Sliding windows
IAsyncPagedEnumerable<TSource[]> WindowPaged(int windowSize);

// Windowed aggregations
IAsyncPagedEnumerable<int> WindowedSumPaged(int windowSize, Func<TSource, int> selector);
IAsyncPagedEnumerable<long> WindowedSumPaged(int windowSize, Func<TSource, long> selector);
IAsyncPagedEnumerable<double> WindowedSumPaged(int windowSize, Func<TSource, double> selector);
IAsyncPagedEnumerable<double> WindowedAveragePaged(int windowSize, Func<TSource, double> selector);
IAsyncPagedEnumerable<TValue> WindowedMinPaged<TValue>(int windowSize, Func<TSource, TValue> selector) where TValue : IComparable<TValue>;
IAsyncPagedEnumerable<TValue> WindowedMaxPaged<TValue>(int windowSize, Func<TSource, TValue> selector) where TValue : IComparable<TValue>;

// Pairwise operations
IAsyncPagedEnumerable<(TSource Previous, TSource Current)> PairwisePaged();
IAsyncPagedEnumerable<TResult> PairwisePaged<TResult>(Func<TSource, TSource, TResult> selector);

// Scan (running aggregation)
IAsyncPagedEnumerable<TAccumulate> ScanPaged<TAccumulate>(TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func);
IAsyncPagedEnumerable<TSource> ScanPaged(Func<TSource, TSource, TSource> func);

⚙️ Performance Considerations

  • Lazy Evaluation — Operators are lazily evaluated; items are only processed when enumerated
  • Pagination Preservation — Pagination metadata flows through the operator chain without recomputation
  • Memory Efficiency — Stream data without materializing entire collections unless needed
  • Cancellation Support — All async operations support cancellation tokens for resource cleanup
  • ValueTask Usage — Terminal async operations use ValueTask<T> for allocation efficiency
  • Materialization Trade-off — Use MaterializeAsync() only for small datasets requiring multiple passes
  • Precomputation Strategy — Use PrecomputePaginationAsync() when pagination metadata is accessed frequently without enumeration
  • Windowing Operations — WindowPaged and related methods use Queue<T> for efficient sliding windows (O(1) enqueue/dequeue)
  • Distinct Operations — DistinctPaged and DistinctByPaged use HashSet<T> for O(1) lookups
  • Analytical Operations — ScanPaged produces running results without storing intermediate states; PairwisePaged has minimal memory footprint

🔗 Dependency

This library depends on:

  • System.AsyncPaged — Core pagination types and interfaces


📄 License & Contributing

Licensed under the Apache License 2.0. Copyright © Kamersoft 2025.

Contributions are welcome! Please visit Xpandables.Net on GitHub to contribute, report issues, or request features.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
10.0.2 166 3/15/2026
10.0.1 145 2/20/2026
10.0.0 159 1/9/2026