public ActionResult GetPageData(int page = 1, [Range(1, int.MaxValue, ErrorMessage = "页大小必须大于0")] int size = 10, string kw = "") { Expression <Func <Advertisement, bool> > where = p => true; if (!string.IsNullOrEmpty(kw)) { where = where.And(p => p.Title.Contains(kw) || p.Description.Contains(kw) || p.Url.Contains(kw)); } var list = AdsService.GetQuery(where).OrderByDescending(p => p.Status == Status.Available).ThenByDescending(a => a.Price).ThenByDescending(a => a.Id).ProjectTo <AdvertisementViewModel>(MapperConfig).NotCacheable().ToPagedList(page, size); return(Ok(list)); }
public ActionResult GetPageData([Range(1, int.MaxValue, ErrorMessage = "页数必须大于0")]int page = 1, [Range(1, int.MaxValue, ErrorMessage = "页大小必须大于0")]int size = 10, string kw = "") { Expression<Func<Advertisement, bool>> where = p => true; if (!string.IsNullOrEmpty(kw)) { where = where.And(p => p.Title.Contains(kw) || p.Description.Contains(kw)); } var query = AdsService.GetQuery(where); var total = query.Count(); var list = query.OrderByDescending(p => p.Price).ThenByDescending(a => a.Weight).Skip((page - 1) * size).Take(size).ProjectTo<AdvertisementViewModel>(MapperConfig).ToList(); var pageCount = Math.Ceiling(total * 1.0 / size).ToInt32(); return PageResult(list, pageCount, total); }
public ActionResult GetPageData([Range(1, int.MaxValue, ErrorMessage = "页数必须大于0")] int page = 1, [Range(1, int.MaxValue, ErrorMessage = "页大小必须大于0")] int size = 10, string kw = "") { Expression <Func <Advertisement, bool> > where = p => true; if (!string.IsNullOrEmpty(kw)) { where = where.And(p => p.Title.Contains(kw) || p.Description.Contains(kw)); } var list = AdsService.GetQuery(where).OrderByDescending(p => p.Price).ThenByDescending(a => a.Weight).ToPagedList <Advertisement, AdvertisementViewModel>(page, size, MapperConfig); var cids = list.Data.Where(m => !string.IsNullOrEmpty(m.CategoryIds)).SelectMany(m => m.CategoryIds.Split(",", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse)).Distinct().ToArray(); var dic = CategoryService.GetQuery(c => cids.Contains(c.Id)).ToDictionary(c => c.Id + "", c => c.Name); foreach (var ad in list.Data.Where(ad => !string.IsNullOrEmpty(ad.CategoryIds))) { ad.CategoryNames = ad.CategoryIds.Split(",").Select(c => dic[c]).Join(","); } return(Ok(list)); }
public async Task <ActionResult> GetPageData([FromServices] ICategoryService categoryService, [Range(1, int.MaxValue, ErrorMessage = "页数必须大于0")] int page = 1, [Range(1, int.MaxValue, ErrorMessage = "页大小必须大于0")] int size = 10, string kw = "") { Expression <Func <Advertisement, bool> > where = p => true; if (!string.IsNullOrEmpty(kw)) { where = where.And(p => p.Title.Contains(kw) || p.Description.Contains(kw) || p.Url.Contains(kw)); } var list = AdsService.GetQuery(where).OrderByDescending(p => p.Status == Status.Available).ThenByDescending(a => a.Price).ToPagedList <Advertisement, AdvertisementViewModel>(page, size, MapperConfig); var cids = list.Data.Where(m => !string.IsNullOrEmpty(m.CategoryIds)).SelectMany(m => m.CategoryIds.Split(",", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse)).Distinct().ToArray(); var dic = await categoryService.GetQuery(c => cids.Contains(c.Id)).ToDictionaryAsync(c => c.Id + "", c => c.Name); foreach (var ad in list.Data.Where(ad => !string.IsNullOrEmpty(ad.CategoryIds))) { ad.CategoryNames = JiebaNet.Segmenter.Common.Extensions.Join(ad.CategoryIds.Split(",").Select(c => dic.GetValueOrDefault(c)), ","); } return(Ok(list)); }