c# – 如何使用.skip(i).take(j)使用lambda表达式导航从数据库中获取给定范围的数据行

我使用以下代码来获取数据以绑定gridview,但是对于性能问题,我只想获得选定的数据行范围,例如1-10表示第一页11-20表示第二页….如何修改以下内容码

 public IQueryable<Employee> SelectEmployees(string SortColumn, string SortOrder, long UserID,---int start,---int end)
    {
            IUnitOfWork objUow;
            var arg = Expression.Parameter(typeof(Employee), "Employee");
            var sortProperty = Expression.Property(arg, SortColumn);
            var lambda = Expression.Lambda(sortProperty, arg);
            var param = Expression.Parameter(typeof(IQueryable<Employee>));
            var orderByCall = Expression.Call(typeof(Queryable), SortOrder, new Type[] { typeof(Employee), sortProperty.Type }, new Expression[] { param, lambda });
            var orderLambda = Expression.Lambda<Func<IQueryable<Employee>, IQueryable<Employee>>>(orderByCall, param).Compile();
            var Ids = objUow.Repository<Users>().Entities.Where((x => x.ID == UserID)).Select(y => y.Id);
            return orderLambda(objUow.Repository<Employee>().Entities.Where(x => x.IsDeleted != true && x.Status == 1 && Ids.Contains(x.CheckID)));
   }

我尝试过skip().take()但它重新出错了. …

 return orderLambda((objUow.Repository<Employee>().Entities.Where(x => x.IsDeleted != true && x.Status == 1 && Ids.Contains(x.CheckID))).Skip(0).Take(10));

EntityFramework.SqlServer.dll中出现“System.NotSupportedException”类型的异常,但未在用户代码中处理

附加信息:方法’Skip’仅支持LINQ to Entities中的排序输入.必须在方法’Skip’之前调用’OrderBy’方法.

我可以做类似下面的事情

 IQueryable<Employee> Obj= orderLambda(objUow.Repository<Employee>().Entities.Where(x => x.IsDeleted != true && x.InvalidStatus == 1 && shopids.Contains(x.CheckID)));
           Obj.Skip(0).Take(10);

最佳答案 每次查询数据库时,您都需要知道要跳过的记录数.

为此,您应该将页码乘以页面大小.

这是一个例子:

int skipAmount = pageNumber * pageSize;
_repository.Where(a => a.IsDeleted != false)
   .OrderBy(a => a.Date)
      .Skip(skipAmount)
         .Take(pageSize);
点赞