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(); 
    }
        //完善后
     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();

 

posted @ 2015-11-16 16:11  丨银魂  阅读(1322)  评论(1)    收藏  举报