[Solved] Cannot implicitly convert type ‘Task’ to ‘Task’


QueryReadOnlyListAsync calls the Dapper extension method QueryAsync, which returns a Task<IEnumerable<T>>. IEnumerable doesn’t implement IReadOnlyList. You can’t return Task<IEnumerable> if the method’s return type is Task<IReadOnlyList>. You need to convert the IEnumerable into a collection that implements IReadOnlyList, such as a regular List:

//Make method async so you can await the Task returned by ExecuteWithResiliency
public async Task<IReadOnlyList<T>> QueryReadOnlyListAsync<T>(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
{
    //Converting using .ToList() for simplicity. Use whatever type suits your use-case best.
    return (await ExecuteWithResiliency((s, p, c) => c.QueryAsync<T>(s, p, transaction, commandTimeout, commandType), sql, param)).ToList();
}

private async Task<T> ExecuteWithResiliency<T>(Func<string, object, SqlConnection, Task<T>> connectionFunc, string sql, object param = null, [CallerMemberName] string operation = "")
{
    return await _resiliencyPolicy.ExecuteAsync(ctx => connectionFunc(sql, param, (SqlConnection)_connection), ContextHelper.NewContext((SqlConnection)_connection, _logger, sql, param, operation));
}

1

solved Cannot implicitly convert type ‘Task>’ to ‘Task>’