[英]List<order>' does not contain a definition for 'GetAwaiter'
I am actually facing a strange problem.我实际上面临一个奇怪的问题。 I am actually trying to return list of data by find specific id.
我实际上是想通过查找特定的 id 返回数据列表。 Everything should work but I don't understand why I am facing this annoying error.
一切都应该有效,但我不明白为什么我会遇到这个烦人的错误。 Here is my code below.
下面是我的代码。
order.cs
: order.cs
:
public class order
{
public int Id { get; set; }
public int? Seid { get; set; }
public AppUser Seuser { get; set; }
public int? Reid { get; set; }
public AppUser Reuser { get; set; }
public string Status { get; set; }
}
Controller: Controller:
[HttpGet]
public async Task <ActionResult<IEnumerable<order>>>GetOrder()
{
var currentuserid = int.Parse(User.GetUserId());
var r = await _orderRepository.GetOrders(currentuserid);
if(r!=null)
{
return Ok(r);
}
return BadRequest();
}
orderRepository:订单库:
public async Task<IEnumerable<order>> GetOrders(int id)
{
return await _context.Orders.Where(x => x.Seid == id).ToList(); //here mainly found error when added await
}
Error:错误:
List<order>
does not contain a definition forGetAwaiter
and no accessible extension methodGetAwaiter
accepting a first argument of typeList<order>
could be found (are you missing a using directive or an assembly reference?) [API]csharp(CS1061)List<order>
不包含GetAwaiter
的定义,并且找不到接受List<order>
类型的第一个参数的可访问扩展方法GetAwaiter
(是否缺少 using 指令或程序集引用?)[API]csharp(CS1061)
When I remove await
to this line of code:- return await _context.Orders.Where(x => x.Seid == id).ToList();
当我将
await
删除到这行代码时:- return await _context.Orders.Where(x => x.Seid == id).ToList();
then error gone.然后错误消失了。 But when I run my application I found a different error just for this
await
case.但是当我运行我的应用程序时,我发现了一个针对这个
await
案例的不同错误。 I am an absolute beginner.我是一个绝对的初学者。 How I can resolve this problem?
我该如何解决这个问题?
You can only await
objects that define GetAwaiter()
.您只能
await
定义GetAwaiter()
的对象。 Try removing the await
, or using ToListAsync()
.尝试删除
await
或使用ToListAsync()
。
As a side note, if your implementation doesn't explicitly require a List
, I'd suggest just returning an IEnumerable:作为旁注,如果您的实现没有明确要求
List
,我建议只返回一个 IEnumerable:
public async Task<IEnumerable<order>> GetOrders(int id)
{
return _context.Orders.Where(x => x.Seid == id);
}
If you are required to return a List
, I'd suggest changing the method signature to reflect that you'll always return a List
:如果您需要返回一个
List
,我建议更改方法签名以反映您将始终返回一个List
:
public async Task<List<order>> GetOrders(int id)
{
return _context.Orders.Where(x => x.Seid == id).ToListAsync();
}
Try to use IAsyncEnumerable<T>
interface that provides asynchronous iteration over values of a specified type:尝试使用
IAsyncEnumerable<T>
接口提供对指定类型值的异步迭代:
public async IAsyncEnumerable<List<order>> GetOrders(int id)
{
yield return await _context.Orders.Where(x => x.Seid == id).ToListAsync<order>();
}
See description of the Compiler Error CS1983请参阅编译器错误 CS1983的说明
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.