MockQueryable | Mocking Entity Framework Core operations such ToListAsync | Form library
kandi X-RAY | MockQueryable Summary
Support
Quality
Security
License
Reuse
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample Here
MockQueryable Key Features
MockQueryable Examples and Code Snippets
Trending Discussions on MockQueryable
Trending Discussions on MockQueryable
QUESTION
I want to implement paging for MVC .NetCore application. I have a list " List<_Item> Items which i got from azure blob" I get the following error when I add paging "IQueryable doesn't implement IAsyncQueryProvider"
Paging I'm trying to Implement: https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/sort-filter-page?view=aspnetcore-3.1
Possible solutions I tried looking at: https://github.com/romantitov/MockQueryable
HomeController: Index function
[Obsolete]
public async Task Index(string outletId, string contractId, string sort, string currentFilter, int? pageNumber)
{
ViewData["CurrentSort"] = sort;
ViewData["OutletId"] = String.IsNullOrEmpty(sort) ? "descending outletId" : "";
ViewData["ContractId"] = sort == "ContractId" ? "descending contractId" : "ContractId";
if (outletId != null || contractId != null)
{
pageNumber = 1;
}
else
{
outletId = currentFilter;
contractId = currentFilter;
}
blobServices.Add_Tags_on_Blob("GSS_EDM_WorkHistoryTerminationsEngagementsIRP5Request_Dev.csv");
// List<_Item> Items = (List<_Item>)blobServices.Get_BlobItem_List();
List<_Item> Items = (List<_Item>)blobServices.Find_Blob_By_Tag(outletId, contractId);
var records = Items.AsQueryable();
switch (sort)
{
case "descending outletId":
records = records.OrderByDescending(x => x.OutletId);
break;
case "descending contractId":
records = records.OrderByDescending(x => x.ContractId);
break;
case "ContractId":
records = records.OrderBy(x => x.ContractId);
break;
default:
records = records.OrderBy(x => x.OutletId);
break;
}
int pageSize = 3;
return View(await PaginatedList<_Item>.CreateAsync(records, pageNumber ?? 1, pageSize));
// return View(Items.ToPagedList(pageNumber ?? 1, 10));
}
Paging Class[Lines of code with error HomeController
public class PaginatedList : List
{
public int PageIndex { get; private set; }
public int TotalPages { get; private set; }
public PaginatedList(List items, int count, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
TotalPages = (int)Math.Ceiling(count / (double)pageSize);
this.AddRange(items);
}
public bool HasPreviousPage
{
get
{
return (PageIndex > 1);
}
}
public bool HasNextPage
{
get
{
return (PageIndex < TotalPages);
}
}
public static async Task> CreateAsync(IQueryable source, int pageIndex, int pageSize)
{
var count = await source.CountAsync();
var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
return new PaginatedList(items, count, pageIndex, pageSize);
}
ANSWER
Answered 2020-Sep-04 at 07:41According to the error message, we could find The provider for the source IQueryable doesn't implement IAsyncQueryProvider. Only providers that implement IAsyncQueryProvider can be used for Entity Framework asynchronous operations.
.
We couldn't let List.AsQueryable()
to use ToListAsync
method, since it doesn't implement the IAsyncQueryProvider.
To solve this issue, you could modify the PaginatedList
method's CreateAsync
method like below:
public static PaginatedList Create(IQueryable source, int pageIndex, int pageSize)
{
var count = source.Count();
var items = source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
return new PaginatedList(items, count, pageIndex, pageSize);
}
Usage:
return View( PaginatedList<_Item>.Create(records, pageNumber ?? 1, pageSize));
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install MockQueryable
Support
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesExplore Kits - Develop, implement, customize Projects, Custom Functions and Applications with kandi kits
Save this library and start creating your kit
Share this Page