IEnumerable' does not contain a definition for 'GetAwaiter' for Async Task Method

c# entity-framework-6

Question

I'm trying to pick 1 record from a table but the error i keep getting is

'IEnumerable' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?)

public class IntermediaryAssignment
    {   
       public string Company{get;set:}
       public string RegistrationNumber{get;set:}
       public bool Dispatched{get;set:}
    }

public async Task<IntermediaryAssignment> PickOneSticker(string company, 
                  string registrationNumber)
    {
        var db = new DatabaseContext();
        var results = await (from s in db.IntermediaryAssignment
                             where s.Dispatched == false && s.CompanyCode == 
                             company && s.RegistrationNumber ==
                              registrationNumber orderby s.StickerCode 
                             ascending select s).ToList().Take(1);
        return results.FirstOrDefault();
    }
1
0
8/26/2018 10:10:30 PM

Accepted Answer

var db = new DatabaseContext();
    var results = await (from s in db.IntermediaryAssignment
                         where s.Dispatched == false && s.CompanyCode == 
                         company && s.RegistrationNumber ==
                          registrationNumber orderby s.StickerCode 
                         ascending select s).ToListAsync());

    return results.FirstOrDefault();
2
8/26/2018 1:08:00 PM

Popular Answer

var results = await (from s in db.IntermediaryAssignment
                         where s.Dispatched == false && s.CompanyCode == 
                         company && s.RegistrationNumber ==
                          registrationNumber orderby s.StickerCode 
                         ascending select s).FirstOrDefaultAsync();
    return results;

If your intention to take first item based on the order its best to use FirstOrDefault without ToList() & Take(1)

FirstOrDefault materialize the query and bring the first item from database.

ToList() if you need to get items based on your filter use to list.

In your current query .ToList().Take(1); The ToList() bring all data to memory and takes the first one, instead of this you can directly fetch first item from db using FirstOrDefault



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow