执行异步分页调用时出错

时间:2018-05-15 14:44:17

标签: c# asp.net-mvc entity-framework asynchronous asp.net-core

我有一个ASP .Net Core MVC应用程序,我在视图中有一个表,该表由实体框架查询中的数据填充。在this guide之后,我实现了对表进行分页的代码。出于某种原因,当从客户端向控制器操作发送表数据请求时,会出现以下错误:

InvalidOperationException: The provider for the source IQueryable doesn't implement IAsyncQueryProvider. Only providers that implement IEntityQueryProvider can be used for Entity Framework asynchronous operations.

以下是控制器操作:

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> GetResultList(ResortDataJoinObj resDeals, int page =1)
        {
            if (ModelState.IsValid)
            {
                var resultsObj = (from rd in _db.ResortData
                                  join ra in _db.ResortAvailability on rd.RecNo equals ra.RecNoDate
                                  where ra.TotalPrice < Int32.Parse(resDeals.priceHighEnd) && ra.TotalPrice > Int32.Parse(resDeals.priceLowEnd)

                                  select new
                                  {
                                      Name = rd.Name,
                                      ImageUrl = rd.ImageUrl,
                                      ResortDetails = rd.ResortDetails,
                                      CheckIn = ra.CheckIn,
                                      Address = rd.Address,
                                      TotalPrice = ra.TotalPrice

                                  });

                int i = 0;
                List<ResortDealResultsObject> resultList = new List<ResortDealResultsObject>();
                foreach (var row in resultsObj)
                {
                        var tempVm = new ResortDealResultsObject
                        {
                            Name = row.Name,
                            ImageUrl = row.ImageUrl,
                            ResortDetails = row.ResortDetails,
                            CheckIn = row.CheckIn,
                            Address = row.Address,
                            TotalPrice = row.TotalPrice
                        };
                        resultList.Add(tempVm);
                }
                int pageSize = 3;
                var model = await PaginatedList<ResortDealResultsObject>.CreateAsync(resultList.AsQueryable(), page, pageSize);

                ResortDataJoinObj joinObj = new ResortDataJoinObj();
                joinObj.PageList = model;
                ViewBag.rowsReturned = true;
                return View(joinObj);
            }
            return View(resDeals);
        }

看起来错误是由行var model = await PaginatedList<ResortDealResultsObject>.CreateAsync(resultList.AsQueryable(), page, pageSize);

引起的

这一行调用了类PaginatedList中的一个方法,该方法在自己的文件中实现(如指南中所述):

public static async Task<PaginatedList<T>> CreateAsync(IQueryable<T> source, int pageIndex, int pageSize)
{
    var count = await source.CountAsync();
    var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
    return new PaginatedList<T>(items, count, pageIndex, pageSize);
}

没有预编译器或编译错误,所以我不确定这里到底出了什么问题,因为我非常仔细地遵循指南。 可能导致错误的原因是什么?

1 个答案:

答案 0 :(得分:1)

List<ResortDealResultsObject> resultList = new List<ResortDealResultsObject>();
            foreach (var row in resultsObj)
            {
                    var tempVm = new ResortDealResultsObject
                    {
                        Name = row.Name,
                        ImageUrl = row.ImageUrl,
                        ResortDetails = row.ResortDetails,
                        CheckIn = row.CheckIn,
                        Address = row.Address,
                        TotalPrice = row.TotalPrice
                    };
                    resultList.Add(tempVm);
            }

此代码不是由实体框架生成的,也不提供异步调用。 调用ToListAsync()方法时已有List,因此没有必要将其强制转换为Queryable并调用ToListAsync()

以下代码应该完成工作

[HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> GetResultList(ResortDataJoinObj resDeals, int page =1)
    {
        if (ModelState.IsValid)
        {
            var resultsObj = from rd in _db.ResortData
                              join ra in _db.ResortAvailability on rd.RecNo equals ra.RecNoDate
                              where ra.TotalPrice < Int32.Parse(resDeals.priceHighEnd) && ra.TotalPrice > Int32.Parse(resDeals.priceLowEnd)

                              select new ResortDealResultsObject
                                 {
                                       Name = rd.Name,
                                       ImageUrl = rd.ImageUrl,
                                       ResortDetails = rd.ResortDetails,
                                       CheckIn = rd.CheckIn,
                                       Address = rd.Address,
                                       TotalPrice = rd.TotalPrice
                                 };

            int pageSize = 3;
            var model = await PaginatedList<ResortDealResultsObject>.CreateAsync(resultsObj, page, pageSize);

            ResortDataJoinObj joinObj = new ResortDataJoinObj();
            joinObj.PageList = model;
            ViewBag.rowsReturned = true;
            return View(joinObj);
        }
        return View(resDeals);
}

我没有对它进行测试,因此可能存在一些编译时错误但逻辑存在:)