.net性能最高的EF分页写法


 /// <summary>
        /// 获取资金流
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetUserCashFlows(int? pageIndex, int? pageSize, int month, string search)
        {
            var result = new ResultDto();
            var userId = (string)ViewBag.UserId;

            try
            {
                Specification<UserCashFlow> specification = new TrueSpecification<UserCashFlow>();

                specification &= new DirectSpecification<UserCashFlow>(m => m.UserId == userId && m.Month == month && !m.Deleted);

                if (!string.IsNullOrEmpty(search))
                {
                    specification &= new DirectSpecification<UserCashFlow>(m => m.Desc.Contains(search));
                }

                var model = _dbContext.UserCashFlows.Where(specification.SatisfiedBy())
                                                    .Select(m => new
                                                    {
                                                        m.Desc,
                                                        m.Cost,
                                                        m.TotalMoney,
                                                        m.OperateDate
                                                    });

                var list = model.OrderByDescending(m => m.OperateDate)
                                .Skip(((int)pageIndex - 1) * (int)pageSize)
                                .Take((int)pageSize)
                                .ToList();

                result.Success = true;
                result.Message = "获取资金流!";
                result.Data = new
                {
                    total = model.Count(),
                    rows = list,
                };
            }
            catch (Exception e)
            {
                result.Message = "获取失败!";
                result.ExMessage = e.Message;
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

上一篇:sql去掉换行符


下一篇:教你如何在map上添加自定义控件