‘List‘ 不包含对 ‘GetAwaiter’ 的定义。

huangapple go评论80阅读模式
英文:

List<order>' does not contain a definition for 'GetAwaiter'

问题

我实际上面临一个奇怪的问题。我实际上正在尝试通过查找特定的ID来返回数据列表。一切都应该正常工作,但我不明白为什么会遇到这个令人烦恼的错误。以下是我的代码。

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:

[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).ToListAsync(); //主要出错的地方是添加了await时
}

错误信息:

List<order> 不包含 GetAwaiter 的定义,也没有可访问的扩展方法 GetAwaiter,接受第一个参数类型为 List<order> (是否缺少 using 指令或程序集引用?) [API]csharp(CS1061)

当我从这行代码中删除 await 时:return await _context.Orders.Where(x => x.Seid == id).ToListAsync();,然后错误消失。但是当我运行我的应用程序时,我发现了一个不同的错误,仅适用于这个 await 情况。我是一个绝对的初学者。我该如何解决这个问题?

英文:

I am actually facing a strange problem. I am actually trying to return list of data by find specific id. Everything should work but I don't understand why I am facing this annoying error. Here is my code below.

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:

[HttpGet]
public async Task &lt;ActionResult&lt;IEnumerable&lt;order&gt;&gt;&gt;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&lt;IEnumerable&lt;order&gt;&gt; GetOrders(int id)
{
   return await _context.Orders.Where(x =&gt; x.Seid == id).ToList(); //here mainly found error when added await
}

Error:

> List&lt;order&gt; does not contain a definition for GetAwaiter and no
> accessible extension method GetAwaiter accepting a first argument of
> type List&lt;order&gt; could be found (are you missing a using directive
> or an assembly reference?) [API]csharp(CS1061)

‘List<order>‘ 不包含对 ‘GetAwaiter’ 的定义。

When I remove await to this line of code:- return await _context.Orders.Where(x =&gt; x.Seid == id).ToList(); then error gone. But when I run my application I found a different error just for this await case. I am an absolute beginner. How I can resolve this problem?

答案1

得分: 0

你只能await定义了GetAwaiter()的对象。尝试移除await,或使用ToListAsync()

另外,如果你的实现不明确需要返回一个List,我建议只返回一个IEnumerable

public async Task<IEnumerable<order>> GetOrders(int id)
{
  return _context.Orders.Where(x => x.Seid == id);
}

如果必须返回一个List,我建议更改方法签名以反映你将始终返回一个List

public async Task<List<order>> GetOrders(int id)
{
  return _context.Orders.Where(x => x.Seid == id).ToListAsync();
}
英文:

You can only await objects that define GetAwaiter().
Try removing the await, or using ToListAsync().

As a side note, if your implementation doesn't explicitly require a List, I'd suggest just returning an IEnumerable:

public async Task&lt;IEnumerable&lt;order&gt;&gt; GetOrders(int id)
 {
   return _context.Orders.Where(x =&gt; 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:

public async Task&lt;List&lt;order&gt;&gt; GetOrders(int id)
 {
   return _context.Orders.Where(x =&gt; x.Seid == id).ToListAsync();
 }

</details>



# 答案2
**得分**: -1

尝试使用 [`IAsyncEnumerable&lt;T&gt;`][1] 接口,它提供了对指定类型的值进行异步迭代的功能:
``` c#
public async IAsyncEnumerable&lt;List&lt;order&gt;&gt; GetOrders(int id)
{
    yield return await _context.Orders.Where(x =&gt; x.Seid == id).ToListAsync&lt;order&gt;();
}

查看 编译器错误 CS1983 的说明。

英文:

Try to use IAsyncEnumerable&lt;T&gt; interface that provides asynchronous iteration over values of a specified type:

public async IAsyncEnumerable&lt;List&lt;order&gt;&gt; GetOrders(int id)
{
    yield return await _context.Orders.Where(x =&gt; x.Seid == id).ToListAsync&lt;order&gt;();
}

See description of the Compiler Error CS1983

huangapple
  • 本文由 发表于 2023年1月8日 23:45:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定