其他分享
首页 > 其他分享> > .net性能最高的EF分页写法

.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);
        }

标签:pageSize,int,specification,EF,result,var,new,net,写法
来源: https://blog.csdn.net/qq_45244974/article/details/122594024