Where am I going wrong in returning IEnumerable <T> from async method?
I have
public static async Task<IEnumerable<ParseTask>> GetArchiveTodos()
{
using(SqlConnection connection = new SqlConnection(SharedInfo.ConnectionString))
using(SqlCommand command = new SqlCommand("GetArchiveTodos", connection))
{
command.CommandType = CommandType.StoredProcedure;
await connection.OpenAsync();
SqlDataReader row = await command.ExecuteReaderAsync();
while(await row.ReadAsync())
{
ParseTask pageToParse = new ParseTask()
{
Id = row.GetInt32(0),
PageType = row.GetString(1),
Html = row.IsDBNull(2) ? null : row.GetString(2),
ThreadId = row.IsDBNull(3) ? null : (int?)row.GetInt32(3),
PageNum = row.GetInt32(4)
};
yield return pageToParse;
}
}
}
and I get the error
Severity Code Description Project File Line Suppression Status Error CS1624. The body of "ArchiveDb.GetArchiveTodos ()" cannot be an iterator block, because "Task>" is not an iterator interface type
source to share
Where am I going wrong in returning IEnumerable from async method?
It's just not supported. You cannot use the returned method as an Task<IEnumerable<T>>
iterator, since you cannot use a keyword yield
. There have been suggestions for this here , but this is not supported yet. The required return type for the iterator is IEnumerable
not Task
- as pointed out in Evk's comment.
source to share