Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 执行异步分页调用时出错_C#_Asp.net Mvc_Entity Framework_Asynchronous_Asp.net Core - Fatal编程技术网

C# 执行异步分页调用时出错

C# 执行异步分页调用时出错,c#,asp.net-mvc,entity-framework,asynchronous,asp.net-core,C#,Asp.net Mvc,Entity Framework,Asynchronous,Asp.net Core,我有一个ASP.Net核心MVC应用程序,其中视图中有一个表,该表由实体框架查询中的数据填充。接下来,我实现了对表进行分页的代码。出于某种原因,当从客户端向控制器操作发送表数据请求时,出现以下错误: InvalidOperationException: The provider for the source IQueryable doesn't implement IAsyncQueryProvider. Only providers that implement IEntityQueryPro

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

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);
        }
没有预编译或编译错误,所以我不确定这里到底出了什么问题,因为我非常仔细地遵循了指南什么可能导致错误?

List resultList=new List();
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);
            }
foreach(resultsObj中的var行) { var tempVm=新的ResortDealResultsObject { Name=row.Name, ImageUrl=row.ImageUrl, RestorDetails=行。RestorDetails, 签入=行。签入, 地址=行。地址, TotalPrice=行。TotalPrice }; 结果列表.Add(tempVm); }
此代码不是由实体框架生成的,并且不提供异步调用。 调用toListSync()方法时,您已经有了一个列表,因此没有必要将其强制转换为Queryable并对其调用toListSync()

下面的代码应该可以完成这项工作

[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);
}
[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务GetResultList(RestortDataJoinObj resDeals,int page=1)
{
if(ModelState.IsValid)
{
var resultsObj=来自rd的数据_db.data
将ra加入到rd.RecNo上的_db.restorAvailability等于ra.RecNoDate
其中ra.TotalPriceInt32.Parse(resDeals.priceLowEnd)
选择新的DealResultsObject
{
Name=rd.Name,
ImageUrl=rd.ImageUrl,
ResortDetails=rd.ResortDetails,
签入=rd.签入,
地址=rd地址,
TotalPrice=rd.TotalPrice
};
int pageSize=3;
var model=await PaginatedList.CreateAsync(resultsObj,page,pageSize);
RestortDataJoinObj joinObj=新的RestortDataJoinObj();
joinObj.PageList=模型;
ViewBag.rowsreturn=true;
返回视图(joinObj);
}
返回视图(resDeals);
}

我没有测试它,因此可能会出现一些编译时错误,但逻辑是:)

您的代码没有分页。它从内存中的数据库加载所有内容,然后将所有内容复制到另一个列表中。根本不要使用
resultList
。如果要从数据库返回
ResortDealResultsObject
,请修改第一个查询以返回
ResortDealResultsObject
,而不是匿名类型。将
resultsObj
传递给PaginatedList构造函数,它已经是iQueryTable了是的,我想我以前在查询中创建对象时尝试过指定类型,但没有成功,但可能当时我做错了什么。
[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);
}