zoukankan      html  css  js  c++  java
  • 数据读写 CommonCurd

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

      

  • 相关阅读:
    PS:图像的编辑(三)修复画笔工具
    如何知道自己电脑连接的无线网密码
    ps里“栅格化图层”有啥作用
    如何解决 ERROR Code 1101报错
    Oracle中SCOTT用户的 emp、dept、bonus、salgrade表的属性和结构
    PSCC2019以上版本如何取消等比裁剪
    distinct 多列详解
    银行取款、查询余额、交电话费功能
    银行卡系统,输入密码取钱功能
    根据周长计算不同形状图形的面积
  • 原文地址:https://www.cnblogs.com/Kuleft/p/11088190.html
Copyright © 2011-2022 走看看