public async Task <QueryResult <Distribution> > ListAsync(DistributionQuery query) { IQueryable <Distribution> queryable = _context.Distributions .Include(p => p.Dataset) .AsNoTracking(); // AsNoTracking tells EF Core it doesn't need to track changes on listed entities. Disabling entity // tracking makes the code a little faster if (query.DatasetId.HasValue && query.DatasetId > 0) { queryable = queryable.Where(p => p.DatasetId == query.DatasetId); } // Here I count all items present in the database for the given query, to return as part of the pagination data. int totalItems = await queryable.CountAsync(); // Here I apply a simple calculation to skip a given number of items, according to the current page and amount of items per page, // and them I return only the amount of desired items. The methods "Skip" and "Take" do the trick here. List <Distribution> distributions = await queryable.Skip((query.Page - 1) *query.ItemsPerPage) .Take(query.ItemsPerPage) .ToListAsync(); // Finally I return a query result, containing all items and the amount of items in the database (necessary for client-side calculations ). return(new QueryResult <Distribution> { Items = distributions, TotalItems = totalItems, }); }
public JsonResult List(int page, int rows, string productName, string shopName, int?status) { if (!string.IsNullOrEmpty(productName)) { productName = productName.Trim(); } if (!string.IsNullOrEmpty(shopName)) { shopName = shopName.Trim(); } DistributionQuery query = new DistributionQuery(); query.PageNo = page; query.PageSize = rows; query.ProductName = productName; query.ShopName = shopName; if (status.HasValue && status.Value != -1) { query.Status = (Himall.Model.ProductBrokerageInfo.ProductBrokerageStatus)status.Value; } var m = _iDistributionService.GetDistributionlist(query); var model = m.Models.ToList(); var dataGrid = new { rows = model, total = m.Total }; return(Json(dataGrid)); }
private string GetCacheKeyForDistributionQuery(DistributionQuery query) { string key = CacheKeys.DistributionList.ToString(); if (query.DatasetId.HasValue && query.DatasetId > 0) { key = string.Concat(key, "_", query.DatasetId.Value); } key = string.Concat(key, "_", query.Page, "_", query.ItemsPerPage); return(key); }
public async Task <QueryResult <Distribution> > ListAsync(DistributionQuery query) { // Here I list the query result from cache if they exist, but now the data can vary according to the dataset ID, page and amount of // items per page. I have to compose a cache to avoid returning wrong data. string cacheKey = GetCacheKeyForDistributionQuery(query); var distributions = await _cache.GetOrCreateAsync(cacheKey, (entry) => { entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(0.01); return(_distributionRepository.ListAsync(query)); }); return(distributions); }
public JsonResult GetProductDataList(string skey = "", int?status = null, int rows = 10, int page = 1) { DistributionQuery query = new DistributionQuery(); query.PageNo = page; query.PageSize = rows; query.ProductName = skey; query.ShopId = curshopid; if (status.HasValue && status.Value != -1) { query.Status = (Himall.Model.ProductBrokerageInfo.ProductBrokerageStatus)status.Value; } var m = _iDistributionService.GetDistributionlist(query); var model = m.Models.ToList(); var result = new { rows = model, total = m.Total }; return(Json(result)); }