dotnet / dotnet/csharplang

[Proposal]: Iterators in lambdas

Open
#9,467 0 comments 0 reactions 1 assignee View on GitHub

@jaredpar is already working on this.

Since Jun 20, 2025.

Proposal champion
Dominant language
C#
Stars
12.7k
Forks
1.1k
Avg merge
11h 1m
Merged PRs (30d)
3

Description

Iterators in lambdas

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.