asp.net-web-api2 - 如何在 web api 2 中为 get 方法编写异步 web api?
问题描述
显示“此异步方法缺少等待”的警告。请建议在我的异步方法中添加等待的位置?
public async Task<IHttpActionResult> GetEmployeeDetails(string id,string StartDate, string EndDate)
{
DateTime sd = Convert.ToDateTime(StartDate);
DateTime ed = Convert.ToDateTime(EndDate);
using (EmployeeEntities db = new EmployeeEntities ())
{
try
{
return Ok(db.employees.Where(x => x.TimeStamp >= sd && x.TimeStamp <= ed && x.DeviceImei ==id).OrderByDescending(x => x.id).ToListAsync());
}
catch (Exception)
{
return BadRequest("Sorry Error Found!!!");
}
}
}
解决方案
您可以在您正在使用的 return 语句中使用它ToListAsync()
return Ok(await db.employees.Where(x => x.TimeStamp >= sd && x.TimeStamp <= ed && x.DeviceImei ==id).OrderByDescending(x => x.id).ToListAsync());
推荐阅读
- c++ - C++ 需要弄清楚为什么这个 for 循环不起作用
- reactjs - 是否可以将用户搜索过滤器添加到 React Admin v3 中的 ReferenceManyField?
- javascript - 谷歌地图自动完成,无法解析输入,因为自动完成添加了 unicode 字符而不是文本
- service - RabbitMQ 交换/队列设计 - 一条或多条消息
- python - Discord Bot Python add_roles
- c# - 如何在VS 2019上使用.net core版本v3.1.0-preview1?
- javascript - 获取整个 DOM 包装边界客户端矩形
- javascript - 通过 HOC 提供数据,但在调用组件属性时,类型“{}”中缺失但类型“”中出现错误
- vb.net - 在 Math.NET Symbolics 中,ArcCos 函数与 SymbolicExpression
- sql-server - 带有日期和 date-1 参数的存储过程