score:7

Accepted answer

ToListAsync() works on a IQueryable<T> only, when you turned it in to a IEnumerable<T> via AsEnumerable() you lost the ability to call it.

Because once you do AsEnumerable() you are working with a in memory collection anyway, just make it a list at that point.

var allZones = await db.zones.Where(d => d.zone_client_id == "15").ToListAsync().ConfigureAwait(false);
return allZones.Distinct<zones>(new zones.Comparer()).ToList();

The last .ToList() is optional. If you don't do it and you enumerate the returned IEnumerable multiple times you will run the Distinct multiple times. Doing a extra .ToList() makes it so it "remembers" the result of the Distinct instead of re-calculating it.

score:2

In your GetApptTask method the result at the return line is an IQueryable which has the extension method ToListAsync defined. The call to GetZoneTask has the AsEnumberable which realizes the IQueryable into an IEnumberable which does NOT have the extension method ToListAsync defined.

public async Task<IEnumerable<appointments>> GetApptTask(){
        var result = db.appointments.Where(d => d.appt_client_id == 15 && d.customer_id == 68009);
        return await result.ToListAsync();//result was an IQueryable here
    }

public async Task <IEnumerable<zones>> GetZoneTask()
    {
        var result = db.zones.Where(d => d.zone_client_id == "15").AsEnumerable().Distinct<zones>(new zones.Comparer());
        return await result.ToListAsync();//here result is an IEnumberable because of the above call to AsEnumberable
    }

score:4

Because ToListAsync() works on a IQueryable only, when you turned it in to a IEnumerable via AsEnumerable() you lost the ability to call it. You can call .AsQueryable() on an IEnumerable sequence and it will return IQueryable.

For example:

private async Task<List<MyModelType>> GetAsyncListFromIEnumerable(
  List<MyModelIEnumerableType> mylist, 
  DateTime startDate, 
  DateTime endDate)
{
  var result = from e in mylist
               where e.StartDate >= startDate 
               && e.EndDate <= endDate
               select new MyModelIEnumerableType
               {
                 // Set of the properties here
               };
  return await result.AsQueryable().ToListAsync();
}

score:0

As @Scott Chamberlain mentioned ToListAsync() works with

IQueryable;

but you can use

FromResult()

method instead of this.

public async Task<IEnumerable<appointments>> GetApptTask()
{

   var result = db.appointments.Where(d => d.appt_client_id == 15 && d.customer_id == 68009);

   return await Task.FromResult(result);
 }

Related Articles