does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'List

Viewed 9985

I hit error does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'List at the return of this method. May I know what I miss out?

[HttpGet]
public async Task<ActionResult<IEnumerable<MovieDto>>> GetMovies()
{
    var movies = (from m in _context.Movies
                    select new MovieDto()
                    {
                        MovieTitle = m.MovieTitle,
                        ReleaseDate = m.ReleaseDate,
                        MovieStatus = m.MovieStatus,
                        PhotoFile = m.PhotoFile
                    }).ToList();

    return await movies;
}
2 Answers

Well ToList() is not async, it doesn't return a Task. Maybe you wanted to use ToListAsync()

[HttpGet]
public async Task<ActionResult<IEnumerable<MovieDto>>> GetMovies()
{
    var movies = (from m in _context.Movies
                    select new MovieDto()
                    {
                        MovieTitle = m.MovieTitle,
                        ReleaseDate = m.ReleaseDate,
                        MovieStatus = m.MovieStatus,
                        PhotoFile = m.PhotoFile
                    }).ToListAsync();

    return Ok(await movies);
}

You could try to run the LINQ query asynchronously to achieve this

[HttpGet]
    public async Task<List<MovieDto>> GetMovies () {
        var moviesTask =
            Task.Factory.StartNew (() => {
                return (from m in _context.Movies 
                        select new MovieDto () {
                          MovieTitle = m.MovieTitle,
                          ReleaseDate = m.ReleaseDate,
                          MovieStatus = m.MovieStatus,
                          PhotoFile = m.PhotoFile
                }).ToList ();
            });
        var movies = await moviesTask;
        return movies;
    }
Related