返回一个条件表达式树的拓展方法

之前做了一个这样的功能,一个页面的查询功能,需要支持很多条件,然后可以点击添加一个条件,类似于Navicat 的这种

代码如下

/// <summary>
        /// 根据条件返回表达式树
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="V"></typeparam>
        /// <param name="propName">字段名</param>
        /// <param name="value"></param>
        /// <param name="ysf">运算符</param>
        /// <returns></returns>
        public static Expression<Func<T, bool>> GenerateExpression<T, V>(string propName, V value, string ysf) where T : class, new()
        {
            Type type = typeof(T);
            //获得参数表达式树
            ParameterExpression parameterExpression = Expression.Parameter(type, "item");
            //分割字段
            string[] propNameArray = propName.Split('.');
            var _propName = propNameArray[0];
            //获取返回类型的公共属性
            int i = 1;
            PropertyInfo propInfo = type.GetProperty(_propName);
            //定义成员表达式树
            MemberExpression memberExpression = Expression.Property(parameterExpression, propInfo);
            //如果字段有导航属性,则执行下面
            for (; i < propNameArray.Length; i++)
            {
                //获取下一级的公共属性
                propInfo = propInfo.PropertyType.GetProperty(propNameArray[i]);
                //获取下一级的成员表达式树
                memberExpression = Expression.Property(memberExpression, propInfo);
            }
            //把参数value转型成和propInfo字段一样的类型
            try
            {
                TypeConverter typeConverter = TypeDescriptor.GetConverter(propInfo.PropertyType);
                var _value = typeConverter.ConvertFrom(value);
                //获取常量表达式树
                ConstantExpression constantExpression = Expression.Constant(_value, propInfo.PropertyType);
                if (ysf == ">")
                {
                    //Expression.GreaterThan表达式树大于比较,成员树memberExpression,常量值树constantExpression,生成二元运算树BinaryExp
                    BinaryExpression BinaryExp = Expression.GreaterThan(memberExpression, constantExpression);
                    return Expression.Lambda<Func<T, bool>>(BinaryExp, new ParameterExpression[] { parameterExpression });
                }
                else if (ysf == ">=")
                {
                    BinaryExpression BinaryExp = Expression.GreaterThanOrEqual(memberExpression, constantExpression);
                    return Expression.Lambda<Func<T, bool>>(BinaryExp, new ParameterExpression[] { parameterExpression });
                }
                else if (ysf == "<")
                {
                    BinaryExpression BinaryExp = Expression.LessThan(memberExpression, constantExpression);
                    return Expression.Lambda<Func<T, bool>>(BinaryExp, new ParameterExpression[] { parameterExpression });
                }
                else if (ysf == "<=")
                {
                    BinaryExpression BinaryExp = Expression.LessThanOrEqual(memberExpression, constantExpression);
                    return Expression.Lambda<Func<T, bool>>(BinaryExp, new ParameterExpression[] { parameterExpression });
                }
                else if (ysf == "=")
                {
                    var methodExpress = Expression.Call(memberExpression, propInfo.PropertyType.GetMethod("Equals", new Type[] { propInfo.PropertyType }), new Expression[] { constantExpression });
                    //返回lamda表达式树
                    return Expression.Lambda<Func<T, bool>>(methodExpress, new ParameterExpression[] { parameterExpression });
                }
                else if (ysf == "like")
                {
                    constantExpression = Expression.Constant(_value, propInfo.PropertyType);
                    var methodExpress = Expression.Call(memberExpression, propInfo.PropertyType.GetMethod("Contains", new Type[] { propInfo.PropertyType }), new Expression[] { constantExpression });
                    //返回lamda表达式树
                    return Expression.Lambda<Func<T, bool>>(methodExpress, new ParameterExpression[] { parameterExpression });
                }
                else
                {
                    new VerifyException("运算符不正确");
                    return null;
                }
            }
            catch (Exception)
            {

                throw new VerifyException($"第{i}个字段的值格式不正确");
            }
        }

 

posted @ 2019-04-13 09:27  嗨小灿  阅读(143)  评论(1编辑  收藏  举报
Live2D