entity framework(EF) 中的Lambda表达式简单应用

entity framework(EF) 中的Lambda表达式简单应用


应用DEMO

List<h_job> list= GetListWhereBy<h_job>(m => m.h_job_id > 2, m => m.h_job_id);


EFModel.dataEntities db;
        public publicDAL()
        {
            this.db = DALBase.GetEntities(); ;
        }
        public publicDAL(EFModel.dataEntities context)
        {
            this.db = context;
        }
        #region 获取列表
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public List<T> GetList<T>() where T : class
        {
            try
            {
                return db.Set<T>().ToList();
            }
            catch (Exception ex)
            {
                WriteLog.WriteErrorLog(ex.ToString());
                return null;
            }
        }

        /// <summary>
        /// 传入条件获取列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public List<T> GetList<T>(Expression<Func<T, bool>> whereLambda) where T : class
        {
            try
            {
                return db.Set<T>().Where(whereLambda).ToList();
            }
            catch (Exception ex)
            {
                WriteLog.WriteErrorLog(ex.ToString());
                return null;
            }
        }

        #endregion

        #region 根据条件排序获取列表
        public List<T> GetListOrderByDesc<T>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, Int32>> orderLambda) where T : class
        {
            try
            {
                return db.Set<T>().Where(whereLambda).OrderByDescending(orderLambda).ToList();
            }
            catch (Exception ex)
            {
                WriteLog.WriteErrorLog(ex.ToString());
                return null;
            }
        }
        public List<T> GetListOrderByDesc<T>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, Int32>> orderLambda, Expression<Func<T, Int32>> orderLambda2) where T : class
        {
            try
            {
                return db.Set<T>().Where(whereLambda).OrderByDescending(orderLambda).OrderByDescending(orderLambda2).ToList();
            }
            catch (Exception ex)
            {
                WriteLog.WriteErrorLog(ex.ToString());
                return null;
            }
        }
        #endregion

        #region 增、改、删
        /// <summary>
        /// 添加
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public RetResult Add<T>(T model) where T : class
        {
            string msg = "操作失败";
            string errorMemo = string.Empty;
            CmdResultError error = CmdResultError.EXCEPTION;
            try
            {
                db.Set<T>().Add(model);
                db.SaveChanges();
                error = CmdResultError.NONE;
                msg = "操作成功";
            }
            catch (Exception ex)
            {
                WriteLog.WriteErrorLog(ex.ToString());
                errorMemo = ex.Message;
            }
            Ret.SetArgument(error, errorMemo, msg);
            return Ret;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public RetResult Edit<T>(T model) where T : class
        {
            string msg = "操作失败";
            string errorMemo = string.Empty;
            CmdResultError error = CmdResultError.EXCEPTION;
            try
            {
                if (db.Entry<T>(model).State == EntityState.Modified)
                {
                    db.SaveChanges();
                }
                else if (db.Entry<T>(model).State == EntityState.Detached)
                {
                    try
                    {
                        db.Set<T>().Attach(model);
                        db.Entry<T>(model).State = EntityState.Modified;
                    }
                    catch (InvalidOperationException)
                    {
                        //T old = Find(model.);
                        //db.Entry(old).CurrentValues.SetValues(model);
                    }
                    db.SaveChanges();
                }
                error = CmdResultError.NONE;
                msg = "操作成功";
            }
            catch (Exception ex)
            {
                WriteLog.WriteErrorLog(ex.ToString());
                errorMemo = ex.Message;
            }
            Ret.SetArgument(error, errorMemo, msg);
            return Ret;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public RetResult Del<T>(T model) where T : class
        {
            string msg = "操作失败";
            string errorMemo = string.Empty;
            CmdResultError error = CmdResultError.EXCEPTION;
            try
            {
                db.Set<T>().Remove(model);
                db.SaveChanges();
                error = CmdResultError.NONE;
                msg = "操作成功";
            }
            catch (Exception ex)
            {
                WriteLog.WriteErrorLog(ex.ToString());
                errorMemo = ex.Message;
            }
            Ret.SetArgument(error, errorMemo, msg);
            return Ret;
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public RetResult Del<T>(List<T> list) where T : class
        {
            string msg = "操作失败";
            string errorMemo = string.Empty;
            CmdResultError error = CmdResultError.EXCEPTION;
            try
            {
                db.Set<T>().RemoveRange(list);
                db.SaveChanges();
                error = CmdResultError.NONE;
                msg = "操作成功";
            }
            catch (Exception ex)
            {
                WriteLog.WriteErrorLog(ex.ToString());
                errorMemo = ex.Message;
            }
            Ret.SetArgument(error, errorMemo, msg);
            return Ret;
        }
        #endregion


参考地址:

http://blog.csdn.net/leftfist/article/details/25005307

阅读更多
个人分类: Entity Framework
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭