简体   繁体   中英

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. 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?

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();
 }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM