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

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

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

问题

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

order.cs:

  1. public class order
  2. {
  3. public int Id { get; set; }
  4. public int? Seid { get; set; }
  5. public AppUser Seuser { get; set; }
  6. public int? Reid { get; set; }
  7. public AppUser Reuser { get; set; }
  8. public string Status { get; set; }
  9. }

Controller:

  1. [HttpGet]
  2. public async Task<ActionResult<IEnumerable<order>>> GetOrder()
  3. {
  4. var currentuserid = int.Parse(User.GetUserId());
  5. var r = await _orderRepository.GetOrders(currentuserid);
  6. if(r != null)
  7. {
  8. return Ok(r);
  9. }
  10. return BadRequest();
  11. }

orderRepository:

  1. public async Task<IEnumerable<order>> GetOrders(int id)
  2. {
  3. return await _context.Orders.Where(x => x.Seid == id).ToListAsync(); //主要出错的地方是添加了await时
  4. }

错误信息:

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:

  1. public class order
  2. {
  3. public int Id { get; set; }
  4. public int? Seid { get; set; }
  5. public AppUser Seuser { get; set; }
  6. public int? Reid { get; set; }
  7. public AppUser Reuser { get; set; }
  8. public string Status { get; set; }
  9. }

Controller:

  1. [HttpGet]
  2. public async Task &lt;ActionResult&lt;IEnumerable&lt;order&gt;&gt;&gt;GetOrder()
  3. {
  4. var currentuserid = int.Parse(User.GetUserId());
  5. var r = await _orderRepository.GetOrders(currentuserid);
  6. if(r!=null)
  7. {
  8. return Ok(r);
  9. }
  10. return BadRequest();
  11. }

orderRepository:

  1. public async Task&lt;IEnumerable&lt;order&gt;&gt; GetOrders(int id)
  2. {
  3. return await _context.Orders.Where(x =&gt; x.Seid == id).ToList(); //here mainly found error when added await
  4. }

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

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

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

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

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:

  1. public async Task&lt;IEnumerable&lt;order&gt;&gt; GetOrders(int id)
  2. {
  3. return _context.Orders.Where(x =&gt; x.Seid == id);
  4. }

If you are required to return a List, I'd suggest changing the method signature to reflect that you'll always return a List:

  1. public async Task&lt;List&lt;order&gt;&gt; GetOrders(int id)
  2. {
  3. return _context.Orders.Where(x =&gt; x.Seid == id).ToListAsync();
  4. }
  5. </details>
  6. # 答案2
  7. **得分**: -1
  8. 尝试使用 [`IAsyncEnumerable&lt;T&gt;`][1] 接口,它提供了对指定类型的值进行异步迭代的功能:
  9. ``` c#
  10. public async IAsyncEnumerable&lt;List&lt;order&gt;&gt; GetOrders(int id)
  11. {
  12. yield return await _context.Orders.Where(x =&gt; x.Seid == id).ToListAsync&lt;order&gt;();
  13. }

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

英文:

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

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

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:

确定