EF查询分页

static List<T> GetPageList(Func<T,bool> whereLambda,Func<T,object> orderLambda,int pageSize,int pageIndex)
        where T:class
    {
        EFEntities context=new EFEntities();//实例化上下文
        var list=context.Set<T>().where(whereLambda).orderByDescending(orderLambda).Skip((pageIndex-1)*pageSize).Take(pageSize).Select(s=>s);
        return list.ToList(); 
    } 

 完善后

//EF lanbda 分页
        public List<dynamic> getPageDate<T, TKey>(Expression<Func<T, dynamic>> select, Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order, int pageIndex, int pageSize, out int Total)
            where T : class
        {
            CIK_NewsEntities db = new CIK_NewsEntities();
            Total = db.Set<T>().Where(where).Count();
            var list = db.Set<T>().Where(where).OrderByDescending(order).Select(select).Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return list.ToList();
        }

使用:

int Total_ = 0;
            rptCate.DataSource = getPageDate<Category, int>(c => new { c.Name, c.CreatedDate, c.CreatedBy }, c => c.Id > 0, c => c.Id, 2, 4, out Total_);
            rptCate.DataBind();
            this.Label1.Text = Total_.ToString();
原文地址:https://www.cnblogs.com/juexin/p/4273490.html