1
votes

This code in .Net Core 3.1, C# 8:

 await  dB.GetListAsync<OrigineDB>();  

results in this error :

IAsyncEnumerable does not contain a definition for 'GetAwaiter'

The answer provided here : https://stackoverflow.com/a/60148747/4180382 didn't help me much since my method contains a yield and a loop like in the example.

What changes should I make ? I don't want to return a List.

 async IAsyncEnumerable<T> GetListAsync<T>() where T : class, new()
        {
            cn = new SqlConnection(cs);
            cn.Open();
            cmd = new SqlCommand(nameProcStock, cn);
            cmd.CommandType = CommandType.StoredProcedure;

            if (parms != null)
                foreach (KeyValuePair<string, object> kvp in parms)
                    cmd.Parameters.AddWithValue(kvp.Key, kvp.Value);

            dr = await cmd.ExecuteReaderAsync();
            while (dr.Read())
                yield return ConvertToObject<T>();
        }
1
IAsyncEnumerable should be used in a foreach loop, then you can await itHoàng Minh Thông
Ok, but how to change dr.Read() to be a foreach loop ? It returns a boolean.Ole EH Dufour
How does GetObjectListAsync<OrigineDB> and GetListAsync<T> related to each other?Pavel Anikhouski
@PavelAnikhouski Corrected the typoOle EH Dufour
@CodeNotFound .net core 3.1Ole EH Dufour

1 Answers

6
votes

If a method returns an object of the type IAsyncEnumerable<T> you can not await it and in fact you don't have to. You can simply use it in a async foreach loop and use it's content in the body of the loop.

You can consume a IAsyncEnumerable<T> like this:

await foreach (var item in db.GetListAsync<OrigineDB>()) 
{
    // do what ever you want
}