krishna mohan
krishna mohan

Reputation: 111

Unable to get linq query data in the async ActionResult with await and task, Error: await can't be used with async method

How to get the LINQ query data in the Task<IHttpActionResult> return type? I have stored the data in var ponddata variable. How to return the data in the Task<IHttpActionResult> action method? I'm getting this error at return Ok statement:

Error CS1061 'IQueryable<>' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'IQueryable<>' could be found (are you missing a using directive or an assembly reference?)

Code:

public async Task<IHttpActionResult> GetAllData(string user)
{
    using (smartpondEntities entities = new smartpondEntities())
    {
        try
        {
            var ponddata = from pond in entities.ponds
                join customerdevice in entities.CustomerDevices on pond.imei equals customerdevice.imei
                join userdata in entities.Users on customerdevice.CustomerId equals userdata.CustomerId
                where userdata.Username == "user1"
                select new { temperature = pond.temp, Imei = pond.imei, timestamp = pond.timestatmp };
            return Ok(await ponddata);
        }
        catch (Exception Ex)
        {
            return BadRequest("Sorry Error Found!!!");
        }
    }
}

Upvotes: 0

Views: 272

Answers (1)

TheGeneral
TheGeneral

Reputation: 81503

The compiler returns the Task for you when you use the async and await pattern, (or more precisely) when you decorate your method with the async keyword

public async Task<IHttpActionResult> GetAllData(string user)

To take advantage of the pattern, you commonly await something in these methods.

Upvotes: 2

Related Questions