与ToList()相比非常慢的ToListAsync() [英] Very slow ToListAsync() compared to ToList()

查看:135
本文介绍了与ToList()相比非常慢的ToListAsync()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有分组和多个包含的复杂LINQ查询.我发现ToListAsync()需要花费更多的时间才能完成,而ToList()只需不到一秒的时间即可返回.

  allReservationsList = allReservations.ToList();//快速地allReservationsList =等待allReservations.ToListAsync();//60秒后再显示一秒 

我做错什么了吗?还是实体框架有问题?

以下是查询:

  var预留= db.Reservations.include(r => r.PickUpLocation).include(r => r.ReturnLocation).Include(r => r.RequestedVehicleModel).ThenInclude(m => m.照片).Include(r => r.RequestedVehicleModel.VehicleType.VehicleModels).Include(r => r.RequestedVehicleType).nnInclude(t => t.Photos).Include(r => r.AssignedUnit.VehicleModel.VehicleType).Include(r => r.DailyPrice.Currency);var allReservations =从r预订orderby r.VersionDate降序通过r.BookingCode将r分组为g选择g; 

解决方案

此SO答案中的报告,如果您正在使用大型列类型(blob, varchar(max)等),实体框架6 使用 ToListAsync()时,效果会不佳.我不能说我100%理解这种情况的复杂性,但是如果您要检索这样的大数据值,则可以说您使用 ToList()是正确的.>

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.

这篇关于与ToList()相比非常慢的ToListAsync()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆