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;
As reported in this SO answer, if you're using large column types (blobs,
varchar(max), etc.), Entity Framework 6 will perform poorly when using
ToListAsync(). I can't say I 100% understand the intricacies which make this the case, but suffice it to say you are correct to use
ToList() if you're retrieving large data values like this.