Exemple #1
0
        public static FilterContainer GenerateNotEqualFilter <T>(this Expression expression) where T : class
        {
            var equalityFilter   = GenerateEqualityFilter <T>(expression);
            var filterDescriptor = new FilterDescriptor <T>();

            return(filterDescriptor.Not(x => equalityFilter));
        }
Exemple #2
0
        protected void parseExpression(Expression expressionTree)
        {
            if (expressionTree == null)
            {
                return;
            }

            if (expressionTree is BinaryExpression && expressionTree.NodeType == ExpressionType.AndAlso &&
                ((BinaryExpression)expressionTree).Left != null)
            {
                parseExpression(((BinaryExpression)expressionTree).Left);
            }

            if (expressionTree is BinaryExpression && expressionTree.NodeType == ExpressionType.AndAlso &&
                ((BinaryExpression)expressionTree).Right != null)
            {
                parseExpression(((BinaryExpression)expressionTree).Right);
            }

            if (expressionTree.NodeType == ExpressionType.Equal || expressionTree.NodeType == ExpressionType.NotEqual)
            {
                BinaryExpression binaryExpression = expressionTree as BinaryExpression;

                string parameterName = "";
                if (binaryExpression.Left.GetType().Equals(typeof(UnaryExpression)))
                {
                    UnaryExpression  unaryExpression = binaryExpression.Left as UnaryExpression;
                    MemberExpression left            = unaryExpression.Operand as MemberExpression;
                    parameterName = left.Member.Name;
                }
                else
                {
                    MemberExpression left = binaryExpression.Left as MemberExpression;
                    parameterName = left.Member.Name;
                }

                string parameterValue = "";
                if (binaryExpression.Right.NodeType == ExpressionType.Convert)
                {
                    UnaryExpression    right              = binaryExpression.Right as UnaryExpression;
                    MemberExpression   memberExpression   = right.Operand as MemberExpression;
                    ConstantExpression constantExpression = memberExpression.Expression as ConstantExpression;
                    object             container          = constantExpression.Value;
                    var member = memberExpression.Member;
                    if (member is FieldInfo)
                    {
                        FieldInfo fieldInfo = member as FieldInfo;
                        if (fieldInfo.FieldType.IsEnum)
                        {
                            object value = ((FieldInfo)member).GetValue(container);
                            //var en = Enum.ToObject(fieldInfo.FieldType, value);
                            var enumValue = (short)value;
                            parameterValue = enumValue.ToString();
                        }
                        else
                        {
                            object value = ((FieldInfo)member).GetValue(container);
                            parameterValue = value.ToString();
                        }
                    }
                    if (member is PropertyInfo)
                    {
                        //object value = ((PropertyInfo)member).GetValue(container, null);
                    }
                }
                else if (binaryExpression.Right.NodeType != ExpressionType.Constant)
                {
                    MemberExpression memberExpression = binaryExpression.Right as MemberExpression;
                    // MemberExpression fieldExpression = memberExpression.Expression as MemberExpression;
                    ConstantExpression right = memberExpression.Expression as ConstantExpression;
                    //parameterValue = right.Value.ToString();

                    object container = right.Value;
                    var    member    = memberExpression.Member;
                    if (member is FieldInfo)
                    {
                        object value = ((FieldInfo)member).GetValue(container);
                        parameterValue = value.ToString();
                    }
                    if (member is PropertyInfo)
                    {
                        //object value = ((PropertyInfo)member).GetValue(container, null);
                    }
                }
                else
                {
                    ConstantExpression right = binaryExpression.Right as ConstantExpression;
                    parameterValue = right.Value.ToString();
                }
                if (expressionTree.NodeType == ExpressionType.Equal)
                {
                    FilterDescriptor <T> filterD = new FilterDescriptor <T>();
                    FilterContainer      filter  = filterD.Term(parameterName.ToCamelCase(), parameterValue);
                    stack.Push(filter);
                }
                else
                {
                    FilterDescriptor <T> filterD = new FilterDescriptor <T>();
                    FilterContainer      filter  = filterD.Not(n => n.Term(parameterName.ToCamelCase(), parameterValue));
                    stack.Push(filter);
                }
            }
            else if (expressionTree.NodeType == ExpressionType.Lambda)
            {
                LambdaExpression     lambdaExpression     = expressionTree as LambdaExpression;
                MethodCallExpression methodCallExpression = lambdaExpression.Body as MethodCallExpression;
                MemberExpression     memberExpression     = methodCallExpression.Object as MemberExpression;
                if (memberExpression.NodeType == ExpressionType.MemberAccess)
                {
                    memberExpression = memberExpression.Expression as MemberExpression;
                }

                ConstantExpression constantExpression = methodCallExpression.Arguments[0] as ConstantExpression;

                string parameterName  = memberExpression.Member.Name;
                string parameterValue = constantExpression.Value.ToString();

                FilterDescriptor <T> filterD    = new FilterDescriptor <T>();
                FilterContainer      baseFilter = filterD.Term(parameterName.ToCamelCase(), parameterValue);

                stack.Push(baseFilter);
            }
            else if (expressionTree.NodeType == ExpressionType.Call)
            {
                MethodCallExpression methodCallExpression = expressionTree as MethodCallExpression;
                Expression           collectionExpression = null;
                MemberExpression     memberExpression     = null;
                if (methodCallExpression != null && methodCallExpression.Method.Name == "Contains")
                {
                    if (methodCallExpression.Method.DeclaringType == typeof(Enumerable))
                    {
                        collectionExpression = methodCallExpression.Arguments[0];
                        memberExpression     = methodCallExpression.Arguments[1] as MemberExpression;
                    }
                    else
                    {
                        collectionExpression = methodCallExpression.Object;
                        memberExpression     = methodCallExpression.Arguments[0] as MemberExpression;
                    }

                    string parameterValue = string.Empty;
                    var    paramvalue     = new List <int>();
                    if (collectionExpression != null && memberExpression != null)
                    {
                        var lambda = Expression.Lambda <Func <List <int> > >(collectionExpression, new ParameterExpression[0]);
                        var value  = lambda.Compile()();
                        parameterValue = memberExpression.Member.Name.ToCamelCase();
                        paramvalue     = value;
                    }
                    FilterDescriptor <T> filterD = new FilterDescriptor <T>();
                    //FilterContainer baseFilter = filterD.Term(memberExpression.Member.Name.ToCamelCase(), paramvalue);
                    FilterContainer baseFilter = filterD.Terms(parameterValue, paramvalue.Select(i => i.ToString()).ToList());

                    stack.Push(baseFilter);
                }
                else
                {
                    memberExpression = methodCallExpression.Object as MemberExpression;
                    if (memberExpression.NodeType == ExpressionType.MemberAccess)
                    {
                        memberExpression = memberExpression.Expression as MemberExpression;
                    }

                    ConstantExpression constantExpression = methodCallExpression.Arguments[0] as ConstantExpression;

                    string parameterName  = memberExpression.Member.Name;
                    string parameterValue = constantExpression.Value.ToString();

                    FilterDescriptor <T> filterD    = new FilterDescriptor <T>();
                    FilterContainer      baseFilter = filterD.Term(parameterName.ToCamelCase(), parameterValue);
                    stack.Push(baseFilter);
                }
            }
            else if (expressionTree.NodeType == ExpressionType.AndAlso)
            {
                //BinaryExpression binaryExpression = expressionTree as BinaryExpression;
                IList <FilterContainer> filters = new List <FilterContainer>();

                foreach (var i in Enumerable.Range(1, stack.Count))
                {
                    FilterContainer filter = stack.Pop();
                    filters.Add(filter);
                }

                FilterDescriptor <T> filterD    = new FilterDescriptor <T>();
                FilterContainer      baseFilter = filterD.And(filters.ToArray());
                stack.Push(baseFilter);
            }
        }