1

Could somebody please explain to me why this code works:

public async IAsyncEnumerable<int> GetAsync()
{
    ...

    var reader = await connection.ExecuteReaderAsync(query, parameters);
    var rowParser = result.GetRowParser<int>();

    while (await reader.ReadAsync())
        yield return rowParser(reader);
}

while this code doesn't (Cannot return a value from iterators)?

public static class DbDataReaderExtensions
{
    public static async IAsyncEnumerable<T> StreamAsync<T>(this DbDataReader reader)
    {
        var rowParser = reader.GetRowParser<T>();
        while (await reader.ReadAsync())
            yield return rowParser(reader);
    }
}

public async IAsyncEnumerable<int> GetAsync()
{
    ...

    var reader = await connection.ExecuteReaderAsync(query, parameters);
    return reader.StreamAsync<int>();
}
toffik325
  • 161
  • 3
  • 11
  • 2
    Related questions: [Return IAsyncEnumerable from an async method](https://stackoverflow.com/questions/59689529/return-iasyncenumerable-from-an-async-method) and [Pass-through for IAsyncEnumerable?](https://stackoverflow.com/questions/59876417/pass-through-for-iasyncenumerable) – Theodor Zoulias Feb 07 '22 at 19:02
  • @Theodor Zoulias, thank you for adding these links. I went through them and I somehow still cannot get the underlying mechanism that disallows the second option and why it could not be simply evaluated by the compiler to be the same as the first one? It puzzles me even more as I have also second function returning ```Task``` and my ```Obj``` has some other properties and ```IAsyncEnumerable``` where I can simply assign result of ```StreamAsync()``` – toffik325 Feb 07 '22 at 19:28
  • 1
    Honestly I don't know the answer to this. You might consider posting a question in the [dotnet/csharplang](https://github.com/dotnet/csharplang/issues) repository, to see if this is a feature that was proposed and rejected for technical reasons, or it was just never proposed. – Theodor Zoulias Feb 07 '22 at 20:55

0 Answers0