3

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'm getting the error

Severity Code Description Project File Line Suppression State Error CS1624 The body of 'ArchiveDb.GetArchiveTodos()' cannot be an iterator block because 'Task>' is not an iterator interface type

user7127000
  • 3,143
  • 6
  • 24
  • 41
  • 5
    You cannot have async methods with iterator block (`yield`), because required return type for iterator block is `IEnumerable`, not `Task`. – Evk Mar 25 '17 at 17:34

1 Answers1

1

Where am I going wrong with returning an IEnumerable from an async method?

This is simply not supported. You cannot have a Task<IEnumerable<T>> returning method be an iterator, i.e.; you cannot use the yield keyword. There has been proposals for this here, but this has yet to be supported. The required return type for an iterator is IEnumerable not Task - as Evk stated in a comment.

David Pine
  • 23,787
  • 10
  • 79
  • 107