I have a complex LINQ query with grouping and multiple includes. I found out that ToListAsync() takes more then second to complete, while ToList() returns in a fraction of second.
allReservationsList = allReservations.ToList(); // fast allReservationsList = await allReservations.ToListAsync(); // more then a second for 60 rows
Am I doing anything wrong or is it an issue with entity framework?
Here is the query:
var reservations = db.Reservations .Include(r => r.PickUpLocation) .Include(r => r.ReturnLocation) .Include(r => r.RequestedVehicleModel).ThenInclude(m => m.Photos) .Include(r => r.RequestedVehicleModel.VehicleType.VehicleModels) .Include(r => r.RequestedVehicleType).ThenInclude(t => t.Photos) .Include(r => r.AssignedUnit.VehicleModel.VehicleType) .Include(r => r.DailyPrice.Currency); var allReservations = from r in reservations orderby r.VersionDate descending group r by r.BookingCode into g select g;
There's too many variables here. However, generally speaking, async methods will take longer to return than sync methods, simply because async has more overhead. Whether that accounts for the entire delay you're seeing or not, there's no way to tell, at least with the information you've provided.