码迷,mamicode.com
首页 > 其他好文 > 详细

Expression树的追加和拼接

时间:2017-05-25 13:41:28      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:class   equal   font   ring   color   span   uid   exp   ble   

封装的类

 public  class WhereHelper<T> where T : class
    {

        private ParameterExpression param;

        private BinaryExpression filter;

        public  WhereHelper()
        {
            param = Expression.Parameter(typeof(T), "c");

            //1==1

            Expression left = Expression.Constant(1);

            filter = Expression.Equal(left, left);

        }
        /// <summary>
        /// 格式化成Expression<Func<T, bool>>
        /// </summary>
        /// <returns></returns>

        public Expression<Func<T, bool>> GetExpression()
        {

            return Expression.Lambda<Func<T, bool>>(filter, param);

        }
     /// <summary>
     /// 等于
     /// </summary>
     /// <param name="propertyName">属性</param>
     /// <param name="value">值</param>
     /// <param name="type">and 还是 or</param>
        public void Equal(string propertyName, object value,string type)
        {

            Expression left = Expression.Property(param, typeof(T).GetProperty(propertyName));

            Expression right = Expression.Constant(value, value.GetType());

            Expression result = Expression.Equal(left, right);
            if (type.ToLower() == "and")
       
                filter = Expression.And(filter, result);
            else
                filter = Expression.Or(filter, result);

        }

        /// <summary>
        /// 包含(模糊查询)
        /// </summary>
        /// <param name="propertyName">属性</param>
        /// <param name="value">值</param>
        /// <param name="type">and 还是 or</param>
        public void Contains(string propertyName, string value,string type)

        {

            Expression left = Expression.Property(param, typeof(T).GetProperty(propertyName));

            Expression right = Expression.Constant(value, value.GetType());

            Expression result = Expression.Call(left, typeof(string).GetMethod("Contains"), right);
            if (type.ToLower() == "and")
                filter = Expression.And(filter, result);
            else
                filter = Expression.Or(filter, result);
        }
    
        /// <summary>
        /// 小于
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public void LessThan(string propertyName, object value, string type)
        {

            Expression left = Expression.Property(param, typeof(T).GetProperty(propertyName));

            Expression right = Expression.Constant(value, value.GetType());

            Expression result = Expression.LessThan(left, right);
            if (type.ToLower() == "and")
                filter = Expression.And(filter, result);
            else
                filter = Expression.Or(filter, result);
        }
        /// <summary>
        /// 小于等于
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public void LessThanOrEqual(string propertyName, object value, string type)
        {

            Expression left = Expression.Property(param, typeof(T).GetProperty(propertyName));

            Expression right = Expression.Constant(value, value.GetType());

            Expression result = Expression.LessThanOrEqual(left, right);
            if (type.ToLower() == "and")
                filter = Expression.And(filter, result);
            else
                filter = Expression.Or(filter, result);
        }
        /// <summary>
        /// 大于
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public void GreaterThan(string propertyName, object value, string type)
        {

            Expression left = Expression.Property(param, typeof(T).GetProperty(propertyName));

            Expression right = Expression.Constant(value, value.GetType());

            Expression result = Expression.GreaterThan(left, right);
            if (type.ToLower() == "and")
                filter = Expression.And(filter, result);
            else
                filter = Expression.Or(filter, result);
        }
        /// <summary>
        ///大于等于
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public void GreaterThanOrEqual(string propertyName, object value, string type)
        {

            Expression left = Expression.Property(param, typeof(T).GetProperty(propertyName));

            Expression right = Expression.Constant(value, value.GetType());

            Expression result = Expression.GreaterThanOrEqual(left, right);
            if (type.ToLower() == "and")
                filter = Expression.And(filter, result);
            else
                filter = Expression.Or(filter, result);
        }
    }

调用的方法类似:

  WhereHelper<UserInfo> user = new WhereHelper<UserInfo>();
            user.Equal("State", EumState.Enable,"and");
            if (!string.IsNullOrEmpty(result))
            {
                user.Contains("Name", result,"and");
                user.Contains("LoginName", result,"or");

            }
            if (!string.IsNullOrEmpty(roleGuid))
            {
                user.Equal("RoleGuid", new Guid(roleGuid),"and");
            }
              
           var list = _userInfoService.GetUserInfo(user.GetExpression(), pageIndex, pageSize);

Expression树的追加和拼接

标签:class   equal   font   ring   color   span   uid   exp   ble   

原文地址:http://www.cnblogs.com/smile1065249097/p/6902992.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!