繁体   English   中英

无法使用 await 和 task 在异步 ActionResult 中获取 linq 查询数据,错误:await 不能与异步方法一起使用

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

如何获取Task<IHttpActionResult>返回类型中的 LINQ 查询数据? 我已将数据存储在var ponddata变量中。 如何在Task<IHttpActionResult>动作方法中返回数据? 我在return Ok语句时收到此错误:

错误 CS1061“IQueryable<>”不包含“GetAwaiter”的定义,并且找不到接受“IQueryable<>”类型的第一个参数的可访问扩展方法“GetAwaiter”(您是否缺少 using 指令或程序集引用? )

代码:

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!!!");
        }
    }
}

当您使用async 和 await 模式时,(或更准确地说)当您使用async关键字装饰您的方法时,编译器会为您返回Task

public async Task<IHttpActionResult> GetAllData(string user)

为了利用该模式,您通常在这些方法中await一些东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM