简体   繁体   English

列表<order> ' 不包含 'GetAwaiter' 的定义</order>

[英]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 for GetAwaiter and no accessible extension method GetAwaiter accepting a first argument of type List<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.

相关问题 async/await &quot;List&lt;&gt; 不包含 &#39;GetAwaiter&#39; 的定义&quot; - async/await "List<> does not contain a definition for 'GetAwaiter'" 不包含&#39;GetAwaiter&#39;的定义 - does not contain a definition for 'GetAwaiter' 列表<MyObject>不包含 GetAwaiter 的定义 - List<MyObject> does not contain a definition for GetAwaiter Task<> 不包含“GetAwaiter”的定义 - Task<> does not contain a definition for 'GetAwaiter' “bool”不包含“GetAwaiter”的定义 - 'bool' does not contain a definition for 'GetAwaiter' &#39;ArrayList&#39;不包含&#39;GetAwaiter&#39;的定义 - 'ArrayList' does not contain a definition for 'GetAwaiter' IQueryable不包含GetAwaiter的定义 - IQueryable does not contain definition for GetAwaiter 不包含“GetAwaiter”的定义,并且没有可访问的扩展方法“GetAwaiter”接受“List”类型的第一个参数 - does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'List HttpResponseMessage' 不包含 'GetAwaiter' 的定义,并且没有可访问的扩展方法 'GetAwaiter' - HttpResponseMessage' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' WebAPI:“ IHttpActionResult”不包含“ GetAwaiter”的定义 - WebAPI : 'IHttpActionResult' does not contain a definition for 'GetAwaiter'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM