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

Time:01-09

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)

enter image description here

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?

CodePudding user response:

Try to use interface that returns a type of Task<T>, e.g. 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

CodePudding user response:

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