Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a Web API controller returning a IQueryable list asynchronous

I've been trying to make the following controller method asynchronous

public IQueryable<Category> GetCategories()
{
    return _context.Categories;
}

I've added the following to make it asynchronous

public async Task<IQueryable<Category>> GetCategories()
{
    return await _context.Categories;
}

The type System.Data.Entity.DbSet is not awaitable

I'm not sure how to get the db context to return all the entities while maintaining the IQueryable.

How do you make the above method asynchronous? Thanks or the help.

like image 418
Gerard Downes Avatar asked Dec 14 '13 14:12

Gerard Downes


2 Answers

It sounds like you're implementing an OData endpoint. If you do it at this level (returning a DbSet directly from your controller), the asynchronous nature is entirely up to WebAPI; all you're doing is returning it an IQueryable that it can use synchronously or asynchronously.

There's nothing you can do to "make" it asynchronous, and there's no reason to.

like image 109
Stephen Cleary Avatar answered Sep 22 '22 21:09

Stephen Cleary


This is the solution I found:

public async Task<IEnumerable<Category>> Get(ODataQueryOptions<Category> options)
{
     var myQueryable = (IQueryable<Category>)options.ApplyTo(context.Categories);   
     return await myQueryable.ToListAsync();
}

It works async and applies the filter.

like image 29
Onur Gumus Avatar answered Sep 24 '22 21:09

Onur Gumus