public partial class CommonCURD<T> where T : class { //DbContext context = new HMOAContainer(); GdWorldDb context = new GdWorldDb(); //增加 public int Add(T userInfo) { context.Set<T>().Add(userInfo); return context.SaveChanges(); } //修改 public int Edit(T userInfo) { context.Entry(userInfo).State = EntityState.Modified; return context.SaveChanges(); } //删除 public int Remove(int id) { T u1 = context.Set<T>().Find(id); context.Set<T>().Remove(u1); return context.SaveChanges(); } public int Remove(int[] ids) { int counter = ids.Length; for (int i = 0; i < counter; i++) { T u1 = context.Set<T>().Find(ids[i]); context.Set<T>().Remove(u1); } return context.SaveChanges(); } public int Remove(T userInfo) { context.Set<T>().Remove(userInfo); return context.SaveChanges(); } //查询 public T GetById(int id) { return context.Set<T>().Find(id); } public IQueryable<T> GetList(Expression<Func<T, bool>> whereLambda) { return context.Set<T>().Where(whereLambda); } public IQueryable<T> GetPageListDESC<Tkey>(Expression<Func<T, bool>> whereLambds, Expression<Func<T, Tkey>> orderLambda, int pageIndex, int pageSize) { return context.Set<T>().Where(whereLambds) .OrderByDescending(orderLambda) .Skip((pageIndex - 1) * pageSize) .Take(pageSize); } public IQueryable<T> GetPageListDESC<Tkey>(Expression<Func<T, bool>> whereLambds, Expression<Func<T, Tkey>> orderLambda, Expression<Func<T, Tkey>> thenorder, int pageIndex, int pageSize) { return context.Set<T>().Where(whereLambds) .OrderByDescending(orderLambda) .ThenByDescending(thenorder) .Skip((pageIndex - 1) * pageSize) .Take(pageSize); } //PagedList分页 public IQueryable<T> GetPageList<TKey, TTKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> order, Expression<Func<T, TTKey>> thenorder, int pageIndex, int pageSize, ref int totalCount, bool IsDesc1 = true, bool IsDesc2 = true) { IQueryable<T> data = null; if (IsDesc1) { if (IsDesc2) { data = context.Set<T>().Where(whereLambda).OrderByDescending(order).ThenByDescending(thenorder).Skip((pageIndex - 1) * pageSize).Take(pageSize).AsNoTracking(); } else { data = context.Set<T>().Where(whereLambda).OrderByDescending(order).OrderBy(thenorder).Skip((pageIndex - 1) * pageSize).Take(pageSize).AsNoTracking(); } } else { if (IsDesc2) { data = context.Set<T>().Where(whereLambda).OrderBy(order).ThenByDescending(thenorder).Skip((pageIndex - 1) * pageSize).Take(pageSize).AsNoTracking(); } else { data = context.Set<T>().Where(whereLambda).OrderBy(order).ThenBy(thenorder).Skip((pageIndex - 1) * pageSize).Take(pageSize).AsNoTracking(); } } totalCount = context.Set<T>().Where(whereLambda).Count(); return data; } }