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

      

  • 相关阅读:
    多元隐函数组求导快速判断自变量和因变量
    jQuery通过ajax方法获取json数据不执行success的原因及解决方法
    JS对于字符串的切割截取
    PHPStorm 解决报错:com.mysql.cj.exceptions.InvalidConnectionAttributeException
    点击checkbox后,$(this).attr('checked')得到的值不会发生改变
    phpstudy等php本地环境运行缓慢的问题解决方法
    HTML5跳转页面并传值以及localStorage的用法
    Js与jQuery的相互转换
    PhpStorm代码编辑区竖线的用途以及如何去掉
    PhpStorm 运行出现502 Bad Gateway
  • 原文地址:https://www.cnblogs.com/Kuleft/p/11088190.html
Copyright © 2011-2022 走看看