csharplang icon indicating copy to clipboard operation
csharplang copied to clipboard

[Proposal]: Iterators in lambdas

Open 333fred opened this issue 6 months ago • 0 comments

Iterators in lambdas

  • Specification: https://github.com/dotnet/csharplang/blob/main/proposals/iterators-in-lambdas.md
  • Discussion: https://github.com/dotnet/csharplang/discussions/9393

Summary

This proposal will remove the restriction that lambda expressions cannot be iterators.


// Desired code
app.MapGet("/search", async IAsyncEnumerable<Product>(
    string query, VectorStoreCollection<int, Product> collection) =>
{
    await foreach (var result in collection.SearchAsync(query, top: 5, new() { Filter = r => r.TenantId == 8 }))
    {
        yield return result.Record;
    }
}

// Actual code
app.MapGet("/search", MapForSearch);

async IAsyncEnumerable<Product> MapForSearch(string query, VectorStoreCollection<int, Product> collection)
{
    await foreach (var result in collection.SearchAsync(query, top: 5, new() { Filter = r => r.TenantId == 8 }))
    {
        yield return result.Record;
    }
}

Design meetings

  • https://github.com/dotnet/csharplang/blob/main/meetings/2025/LDM-2025-06-18.md#iterators-in-lambdas

333fred avatar Jun 20 '25 21:06 333fred