1. 程式人生 > >自己編寫的 entity framwork 6 通用 dal 類 增刪改查 多分類欄位

自己編寫的 entity framwork 6 通用 dal 類 增刪改查 多分類欄位

  public class BaseDAL<T> where T : class,new()
    {
        string strConn = "";
        public BaseDAL(string connString)
        {
            strConn = connString;   
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity">實體</param>
        /// <returns>返回受影響行數</returns>
        public bool Add(T entity)
        {
            using (SysDb db = new SysDb(strConn))
            {
                db.Entry<T>(entity).State = EntityState.Added;
                return db.SaveChanges() > 0;

            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">實體</param>
        /// <returns>返回受影響行數</returns>
        public bool Update(T entity)
        {
            using (SysDb db = new SysDb(strConn))
            {
                db.Set<T>().Attach(entity);
                db.Entry<T>(entity).State = EntityState.Modified;
                return db.SaveChanges() > 0;
            }
        }

        /// <summary>
        /// 刪除
        /// </summary>
        /// <param name="entity">實體</param>
        /// <returns>返回受影響行數</returns>
        public bool Delete(T entity)
        {
            using (SysDb db = new SysDb(strConn))
            {
                db.Set<T>().Attach(entity);
                db.Entry<T>(entity).State = EntityState.Deleted;
                return db.SaveChanges() > 0;
            }
        }

        /// <summary>
        /// 根據條件刪除
        /// </summary>
        /// <param name="deleWhere">刪除條件</param>
        /// <returns>返回受影響行數</returns>
        public bool DeleteByConditon(Expression<Func<T, bool>> deleWhere)
        {
            using (SysDb db = new SysDb(strConn))
            {
                List<T> entitys = db.Set<T>().Where(deleWhere).ToList();
                entitys.ForEach(m => db.Entry<T>(m).State = EntityState.Deleted);
                return db.SaveChanges() > 0;
            }
        }

        /// <summary>
        /// 查詢單個
        /// </summary>
        /// <param name="id">主鍵</param>
        /// <returns></returns>
        public T GetSingleById(int id)
        {
            using (SysDb db = new SysDb(strConn))
            {
                return db.Set<T>().Find(id);
            }
        }

        /// <summary>
        /// 查詢單個
        /// </summary>
        /// <param name="seleWhere">查詢條件</param>
        /// <returns></returns>
        public T GetSingle(Expression<Func<T, bool>> seleWhere)
        {
            using (SysDb db = new SysDb(strConn))
            {
                return db.Set<T>().AsExpandable().FirstOrDefault(seleWhere);
            }
        }


        /// <summary>
        /// 獲取所有實體集合
        /// </summary>
        /// <returns></returns>
        public List<T> GetAll()
        {
            using (SysDb db = new SysDb(strConn))
            {
                return db.Set<T>().AsExpandable().ToList<T>();
            }
        }

        /// <summary>
        /// 獲取所有實體集合(單個排序)
        /// </summary>
        /// <returns></returns>
        public List<T> GetAll<Tkey>(Expression<Func<T, Tkey>> orderWhere,bool isDesc)
        {
            using (SysDb db = new SysDb(strConn))
            {
                return CommonSort(db.Set<T>().AsExpandable(), orderWhere, isDesc).ToList<T>();
            }
        }

        /// <summary>
        /// 獲取所有實體集合(多個排序)
        /// </summary>
        /// <returns></returns>
        public List<T> GetAll(params OrderModelField[] orderByExpression)
        {
            using (SysDb db = new SysDb(strConn))
            {
                return CommonSort(db.Set<T>().AsExpandable(), orderByExpression).ToList();
            }
        }

        /// <summary>
        /// 單個排序通用方法
        /// </summary>
        /// <typeparam name="Tkey">排序欄位</typeparam>
        /// <param name="data">要排序的資料</param>
        /// <param name="orderWhere">排序條件</param>
        /// <param name="isDesc">是否倒序</param>
        /// <returns>排序後的集合</returns>
        public IQueryable<T> CommonSort<Tkey>(IQueryable<T> data, Expression<Func<T, Tkey>> orderWhere, bool isDesc)
        {
            if (isDesc)
            {
                return data.OrderByDescending(orderWhere);
            }
            else
            {
                return data.OrderBy(orderWhere);
            }
        }

        /// <summary>
        /// 多個排序通用方法
        /// </summary>
        /// <typeparam name="Tkey">排序欄位</typeparam>
        /// <param name="data">要排序的資料</param>
        /// <param name="orderWhereAndIsDesc">字典集合(排序條件,是否倒序)</param>
        /// <returns>排序後的集合</returns>
        public IQueryable<T> CommonSort(IQueryable<T> data, params OrderModelField[] orderByExpression)
        {
            //建立表示式變數引數
            var parameter = Expression.Parameter(typeof(T), "o");

            if (orderByExpression != null && orderByExpression.Length > 0)
            {
                for (int i = 0; i < orderByExpression.Length; i++)
                {
                    //根據屬性名獲取屬性
                    var property = typeof(T).GetProperty(orderByExpression[i].PropertyName);
                    //建立一個訪問屬性的表示式
                    var propertyAccess = Expression.MakeMemberAccess(parameter, property);
                    var orderByExp = Expression.Lambda(propertyAccess, parameter);

                    string OrderName = "";
                    if (i > 0)
                    {
                        OrderName = orderByExpression[i].IsDESC ? "ThenByDescending" : "ThenBy";
                    }
                    else
                        OrderName = orderByExpression[i].IsDESC ? "OrderByDescending" : "OrderBy";

                    MethodCallExpression resultExp = Expression.Call(typeof(Queryable), OrderName, new Type[] { typeof(T), property.PropertyType }, 
                        data.Expression, Expression.Quote(orderByExp));

                    data = data.Provider.CreateQuery<T>(resultExp);
                }
            }
           return data;
        }

        /// <summary>
        /// 根據條件查詢實體集合
        /// </summary>
        /// <param name="seleWhere">查詢條件 lambel表示式</param>
        /// <returns></returns>
        public List<T> GetList(Expression<Func<T, bool>> seleWhere)
        {
            using (SysDb db = new SysDb(strConn))
            {
                return db.Set<T>().AsExpandable().Where(seleWhere).ToList();
            }
        }

        /// <summary>
        /// 根據條件查詢實體集合(單個欄位排序)
        /// </summary>
        /// <param name="seleWhere">查詢條件 lambel表示式</param>
        /// <returns></returns>
        public List<T> GetList<Tkey>(Expression<Func<T, bool>> seleWhere, Expression<Func<T, Tkey>> orderWhere,bool isDesc)
        {
            using (SysDb db = new SysDb(strConn))
            {
                return CommonSort(db.Set<T>().AsExpandable().Where(seleWhere),orderWhere,isDesc).ToList();
            }
        }

        /// <summary>
        /// 根據條件查詢實體集合(多個欄位排序)
        /// </summary>
        /// <param name="seleWhere">查詢條件 lambel表示式</param>
        /// <returns></returns>
        public List<T> GetList(Expression<Func<T, bool>> seleWhere, params OrderModelField[] orderByExpression)
        {
            using (SysDb db = new SysDb(strConn))
            {
                return CommonSort(db.Set<T>().AsExpandable().Where(seleWhere), orderByExpression).ToList();
            }
        }

        /// <summary>
        /// 獲取分頁集合(無條件無排序)
        /// </summary>
        /// <returns></returns>
        public List<T> GetListPaged<Tkey>(int pageIndex, int pageSize, out int totalcount)
        {
            using (SysDb db = new SysDb(strConn))
            {
                totalcount = db.Set<T>().AsExpandable().Count();//獲取總數
                //需要增加AsExpandable(),否則查詢的是所有資料到記憶體,然後再排序  AsExpandable是linqkit.dll中的方法
                return db.Set<T>().AsExpandable().Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            }
        }

        /// <summary>
        /// 獲取分頁集合(無條件單個排序)
        /// </summary>
        /// <returns></returns>
        public List<T> GetListPaged<Tkey>(int pageIndex, int pageSize, Expression<Func<T, Tkey>> orderWhere,bool isDesc,out int totalcount)
        {
            using (SysDb db = new SysDb(strConn))
            {
                totalcount = db.Set<T>().AsExpandable().Count();//獲取總數
                //需要增加AsExpandable(),否則查詢的是所有資料到記憶體,然後再排序  AsExpandable是linqkit.dll中的方法
                return CommonSort(db.Set<T>().AsExpandable(), orderWhere, isDesc).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            }
        }

        /// <summary>
        /// 獲取分頁集合(無條件多欄位排序)
        /// </summary>
        /// <returns></returns>
        public List<T> GetListPaged(int pageIndex, int pageSize, out int totalcount, params OrderModelField[] orderByExpression)
        {
            using (SysDb db = new SysDb(strConn))
            {
                totalcount = db.Set<T>().AsExpandable().Count();//獲取總數
                //需要增加AsExpandable(),否則查詢的是所有資料到記憶體,然後再排序  AsExpandable是linqkit.dll中的方法
                return CommonSort(db.Set<T>().AsExpandable(), orderByExpression).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            }
        }

        /// <summary>
        /// 獲取分頁集合(有條件無排序)
        /// </summary>
        /// <returns></returns>
        public List<T> GetListPaged<Tkey>(int pageIndex, int pageSize, Expression<Func<T, bool>> seleWhere,out int totalcount)
        {
            using (SysDb db = new SysDb(strConn))
            {
                totalcount = db.Set<T>().AsExpandable().Where(seleWhere).Count();//獲取總數
                //需要增加AsExpandable(),否則查詢的是所有資料到記憶體,然後再排序  AsExpandable是linqkit.dll中的方法
                return db.Set<T>().AsExpandable().Where(seleWhere).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            }
        }

        /// <summary>
        /// 獲取分頁集合(有條件單個排序)
        /// </summary>
        /// <returns></returns>
        public List<T> GetListPaged<Tkey>(int pageIndex, int pageSize, Expression<Func<T, bool>> seleWhere, Expression<Func<T, Tkey>> orderWhere, bool isDesc, out int totalcount)
        {
            using (SysDb db = new SysDb(strConn))
            {
                totalcount = db.Set<T>().AsExpandable().Where(seleWhere).Count();//獲取總數
                //需要增加AsExpandable(),否則查詢的是所有資料到記憶體,然後再排序  AsExpandable是linqkit.dll中的方法
                return CommonSort(db.Set<T>().AsExpandable().Where(seleWhere), orderWhere, isDesc).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            }
        }

        /// <summary>
        /// 獲取分頁集合(有條件多欄位排序)
        /// </summary>
        /// <returns></returns>
        public List<T> GetListPaged(int pageIndex, int pageSize, Expression<Func<T, bool>> seleWhere, out int totalcount,params OrderModelField[] orderModelFiled)
        {
            using (SysDb db = new SysDb(strConn))
            {
                totalcount = db.Set<T>().AsExpandable().Where(seleWhere).Count();//獲取總數
                //需要增加AsExpandable(),否則查詢的是所有資料到記憶體,然後再排序  AsExpandable是linqkit.dll中的方法
                return CommonSort(db.Set<T>().AsExpandable().Where(seleWhere), orderModelFiled).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            }
        }

        /// <summary>
        /// 增加rownum列
        /// </summary>
        /// <param name="lsTemp"></param>
        /// <returns></returns>
        public List<T> AddRowNum(List<T> lsTemp) 
        {
            if (typeof(T).GetProperty("RowNum") != null)
            {
                int i=0;
                foreach (var item in lsTemp)
                {
                    typeof(T).GetProperty("RowNum").SetValue(item, i, null);
                    i++;
                }
            }
            return lsTemp;
        }

    }
    public struct OrderModelField
    {
        public string PropertyName { get; set; }
        public bool IsDESC { get; set; }
    }