C#通用表达式树

csharp

浏览数:210

2019-1-7

AD:资源代下载服务
    //通用Lambda表达式类
    public class LambdaExtention<T>where T:class
    {
        private List<Expression> myExpressionList;
        private ParameterExpression myParameter;

        public LambdaExtention()
        {
            myExpressionList = new List<Expression>();
            //创建表达式参数
            myParameter = Expression.Parameter(typeof(T), "x");
            ////打印x
            //Debug.Print($"LambdaExtention.myParameter:{myParameter.ToString()}");
        }

        //字符串Contains筛选
        public void Contains(Expression<Func<T,string>>expProperty,object value)
        {
            ////打印类似Contains.expProperty.Body:x.PropertyOrField
            //Debug.Print($"Contains.expProperty.Body:{expProperty.Body.ToString()}");
            Expression expRes = Expression.Call(expProperty.Body,
                typeof(string).GetMethod("Contains"),Expression.Constant(value));
            ////打印类似Contains.expRes:x.PropertyOrField.Contains("value")
            //Debug.Print($"Contains.expRes:{expRes.ToString()}");
            myExpressionList.Add(expRes);
        }

        //等于
        public void Equal(Expression<Func<T,object>>expProperty,object value)
        {
            var member = GetMemberExpression(expProperty);
            Expression expRes= Expression.Equal(member, Expression.Constant(value, member.Type));
            ////打印Equal.expRes:(x.CmsSn==value)
            //Debug.Print($"Equal.expRes:{expRes.ToString()}");
            myExpressionList.Add(expRes);
        }

        //小于
        public void LessThan(Expression<Func<T, object>> expProperty, object value)
        {
            var member = GetMemberExpression(expProperty);
            Expression expRes = Expression.LessThan(member, Expression.Constant(value, member.Type));
            myExpressionList.Add(expRes);
        }

        //小于等于
        public void LessThanOrEqual(Expression<Func<T, object>> expProperty, object value)
        {
            var member = GetMemberExpression(expProperty);
            Expression expRes = Expression.LessThanOrEqual(member, Expression.Constant(value, member.Type));
            myExpressionList.Add(expRes);
        }

        //大于
        public void GreaterThan(Expression<Func<T, object>> expProperty, object value)
        {
            var member = GetMemberExpression(expProperty);
            Expression expRes = Expression.GreaterThan(member, Expression.Constant(value, member.Type));
            myExpressionList.Add(expRes);
        }

        //大于等于
        public void GreaterThanOrEqual(Expression<Func<T, object>> expProperty, object value)
        {
            var member = GetMemberExpression(expProperty);
            Expression expRes = Expression.GreaterThanOrEqual(member, Expression.Constant(value, member.Type));
            myExpressionList.Add(expRes);
        }

        //获取多个条件相&的Lambda
        public Expression<Func<T, bool>> GetAndLambda()
        {
            Expression whereExpr = null;
            foreach (var expr in myExpressionList)
            {
                if (whereExpr == null)
                    whereExpr = expr;
                else
                    whereExpr = Expression.And(whereExpr, expr);
            }
            if (whereExpr == null)
                return null;

            return Expression.Lambda<Func<T, Boolean>>(whereExpr,myParameter);
        }

        //获取多个条件相|的Lambda
        public Expression<Func<T, bool>> GetOrLambda()
        {
            Expression whereExpr = null;
            foreach (var expr in myExpressionList)
            {
                if (whereExpr == null)
                    whereExpr = expr;
                else
                    whereExpr = Expression.Or(whereExpr, expr);
            }
            if (whereExpr == null)
                return null;

            return Expression.Lambda<Func<T, Boolean>>(whereExpr, myParameter);
        }        //获取MemberExpression,类似x.PropertyOrField
        private MemberExpression GetMemberExpression(Expression<Func<T,object>>exp)
        {
            ////打印类似GetMemberExpression.exp:x =>(x.CmsSn==value),exp.Body:(x.CmsSn==value)
            //Debug.Print($"GetMemberExpression.exp:{exp.ToString()},exp.Body:{exp.Body.ToString()}");
            var arrSplit = exp.Body.ToString().Split("(.)".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            var property = arrSplit[arrSplit.Length - 1];
            MemberExpression member = Expression.PropertyOrField(myParameter, property);
            ////打印类似GetMemberExpression.member:x.CmsSn
            //Debug.Print($"GetMemberExpression.member:{member.ToString()}");
            return member;
        }
    }