英文:
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 <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)
When I remove await
to this line of code:- 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. 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<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
:
public async Task<List<order>> GetOrders(int id)
{
return _context.Orders.Where(x => x.Seid == id).ToListAsync();
}
</details>
# 答案2
**得分**: -1
尝试使用 [`IAsyncEnumerable<T>`][1] 接口,它提供了对指定类型的值进行异步迭代的功能:
``` c#
public async IAsyncEnumerable<List<order>> GetOrders(int id)
{
yield return await _context.Orders.Where(x => x.Seid == id).ToListAsync<order>();
}
查看 编译器错误 CS1983 的说明。
英文:
Try to use IAsyncEnumerable<T>
interface that provides asynchronous iteration over values of a specified type:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论