Beispiel #1
0
        private Expression GetExpression(Expression binaryExpression, ColumnFilterValue value, Type targetType,
                                         MethodInfo removeDiacritics)
        {
            IFilterType filterType = _typeResolver.GetFilterType(targetType);

            //support nullable types:
            Expression firstExpr = IsNullable
                                       ? Expression.Property(_expression.Body, _pi.PropertyType.GetProperty("Value"))
                                       : _expression.Body;

            var filterExpression = filterType.GetFilterExpression(firstExpr, value.FilterValue, value.FilterType, removeDiacritics);

            if (filterExpression == null)
            {
                return(null);
            }

            if (IsNullable && targetType != typeof(string))
            {
                //add additional filter condition for check items on NULL with invoring "HasValue" method.
                //for example: result of this expression will like - c=> c.HasValue && c.Value = 3
                MemberExpression hasValueExpr = Expression.Property(_expression.Body,
                                                                    _pi.PropertyType.GetProperty("HasValue"));
                filterExpression = Expression.AndAlso(hasValueExpr, filterExpression);
            }


            binaryExpression = binaryExpression == null ? filterExpression :
                               Expression.AndAlso(binaryExpression, filterExpression);

            //return filter expression
            return(binaryExpression);
        }
Beispiel #2
0
        public IQueryable <T> ApplyFilter(IQueryable <T> items, ColumnFilterValue value)
        {
            if (value == ColumnFilterValue.Null)
            {
                throw new ArgumentNullException("value");
            }

            var pi = (PropertyInfo)((MemberExpression)_expression.Body).Member;
            Expression <Func <T, bool> > expr = GetFilterExpression(pi, value);

            if (expr == null)
            {
                return(items);
            }
            return(items.Where(expr));
        }
Beispiel #3
0
        private string GetFilterString(ColumnFilterValue value)
        {
            string result = "";

            // get column name
            List <string> names      = new List <string>();
            Expression    expression = _expression.Body;

            while (expression.NodeType != ExpressionType.Parameter)
            {
                names.Add(((MemberExpression)expression).Member.Name);
                expression = ((MemberExpression)expression).Expression;
            }
            for (int i = names.Count - 1; i >= 0; i--)
            {
                result += names[i];
                if (i != 0)
                {
                    result += "/";
                }
            }

            if (!string.IsNullOrWhiteSpace(result))
            {
                if (value.FilterType == GridFilterType.IsNull)
                {
                    result += " eq null";
                }
                else if (value.FilterType == GridFilterType.IsNotNull)
                {
                    result += " ne null";
                }
                else
                {
                    //get target type:
                    Type targetType = IsNullable ? Nullable.GetUnderlyingType(_pi.PropertyType) : _pi.PropertyType;

                    IFilterType filterType = _typeResolver.GetFilterType(targetType);
                    result = filterType.GetFilterExpression(result, value.FilterValue, value.FilterType);
                }
            }

            return(result);
        }
Beispiel #4
0
        private Expression <Func <T, bool> > GetFilterExpression(PropertyInfo pi, ColumnFilterValue value)
        {
            //detect nullable
            bool isNullable = pi.PropertyType.GetTypeInfo().IsGenericType&&
                              pi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>);
            //get target type:
            Type targetType = isNullable ? Nullable.GetUnderlyingType(pi.PropertyType) : pi.PropertyType;

            IFilterType filterType = _typeResolver.GetFilterType(targetType);

            //build expression to filter collection:
            ParameterExpression entityParam = _expression.Parameters[0];
            //support nullable types:
            Expression firstExpr = isNullable
                                       ? Expression.Property(_expression.Body, pi.PropertyType.GetProperty("Value"))
                                       : _expression.Body;

            Expression binaryExpression = filterType.GetFilterExpression(firstExpr, value.FilterValue, value.FilterType);

            if (binaryExpression == null)
            {
                return(null);
            }

            if (targetType == typeof(string))
            {
                //check for strings, they may be NULL
                //It's ok for ORM, but throw exception in linq to objects. Additional check string on null
                Expression nullExpr = Expression.NotEqual(_expression.Body, Expression.Constant(null));
                binaryExpression = Expression.AndAlso(nullExpr, binaryExpression);
            }
            else if (isNullable)
            {
                //add additional filter condition for check items on NULL with invoring "HasValue" method.
                //for example: result of this expression will like - c=> c.HasValue && c.Value = 3
                MemberExpression hasValueExpr = Expression.Property(_expression.Body,
                                                                    pi.PropertyType.GetProperty("HasValue"));
                binaryExpression = Expression.AndAlso(hasValueExpr, binaryExpression);
            }
            //return filter expression
            return(Expression.Lambda <Func <T, bool> >(binaryExpression, entityParam));
        }
Beispiel #5
0
        private Expression GetExpression(PropertyInfo pi, ColumnFilterValue value)
        {
            //detect nullable
            bool isNullable = pi.PropertyType.GetTypeInfo().IsGenericType&&
                              pi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>);
            //get target type:
            Type targetType = isNullable ? Nullable.GetUnderlyingType(pi.PropertyType) : pi.PropertyType;

            List <string> names      = new List <string>();
            Expression    expression = _expression.Body;

            while (expression.NodeType != ExpressionType.Parameter)
            {
                names.Add(((MemberExpression)expression).Member.Name);
                expression = ((MemberExpression)expression).Expression;
            }

            Expression binaryExpression = null;

            for (int i = names.Count - 1; i >= 0; i--)
            {
                expression = Expression.Property(expression, names[i]);

                var nestedPi = (PropertyInfo)((MemberExpression)expression).Member;

                //detect nullable
                bool nestedIsNullable = nestedPi.PropertyType.GetTypeInfo().IsGenericType&&
                                        nestedPi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>);
                //get target type:
                Type nestedTargetType = nestedIsNullable ? Nullable.GetUnderlyingType(nestedPi.PropertyType) : nestedPi.PropertyType;

                // Check for null on nested properties and target object if it's a string
                // It's ok for ORM, but throw exception in linq to objects
                if (nestedIsNullable || nestedTargetType == typeof(string))
                {
                    binaryExpression = binaryExpression == null?
                                       Expression.NotEqual(expression, Expression.Constant(null)) :
                                           Expression.AndAlso(binaryExpression, Expression.NotEqual(expression, Expression.Constant(null)));
                }
            }

            IFilterType filterType = _typeResolver.GetFilterType(targetType);

            //support nullable types:
            Expression firstExpr = isNullable
                                       ? Expression.Property(_expression.Body, pi.PropertyType.GetProperty("Value"))
                                       : _expression.Body;

            var filterExpression = filterType.GetFilterExpression(firstExpr, value.FilterValue, value.FilterType);

            if (filterExpression == null)
            {
                return(null);
            }

            if (isNullable && targetType != typeof(string))
            {
                //add additional filter condition for check items on NULL with invoring "HasValue" method.
                //for example: result of this expression will like - c=> c.HasValue && c.Value = 3
                MemberExpression hasValueExpr = Expression.Property(_expression.Body,
                                                                    pi.PropertyType.GetProperty("HasValue"));
                filterExpression = Expression.AndAlso(hasValueExpr, filterExpression);
            }


            binaryExpression = binaryExpression == null ? filterExpression :
                               Expression.AndAlso(binaryExpression, filterExpression);

            //return filter expression
            return(binaryExpression);
        }
Beispiel #6
0
        private Expression GetExpression(ColumnFilterValue value, MethodInfo removeDiacritics)
        {
            //get target type:
            Type targetType = IsNullable ? Nullable.GetUnderlyingType(_pi.PropertyType) : _pi.PropertyType;

            List <string> names      = new List <string>();
            Expression    expression = _expression.Body;

            while (expression.NodeType != ExpressionType.Parameter)
            {
                names.Add(((MemberExpression)expression).Member.Name);
                expression = ((MemberExpression)expression).Expression;
            }

            Expression binaryExpression = null;

            for (int i = names.Count - 1; i >= 0; i--)
            {
                expression = Expression.Property(expression, names[i]);

                var nestedPi = (PropertyInfo)((MemberExpression)expression).Member;

                //detect nullable
                bool nestedIsNullable = nestedPi.PropertyType.GetTypeInfo().IsGenericType&&
                                        nestedPi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>);
                //get target type:
                Type nestedTargetType = nestedIsNullable ? Nullable.GetUnderlyingType(nestedPi.PropertyType) : nestedPi.PropertyType;

                // Check for null on nested properties and not value type (string and objects are reference type)
                // It's ok for ORM, but throw exception in linq to objects
                if (nestedIsNullable || !nestedTargetType.IsValueType)
                {
                    binaryExpression = binaryExpression == null?
                                       Expression.NotEqual(expression, Expression.Constant(null)) :
                                           Expression.AndAlso(binaryExpression, Expression.NotEqual(expression, Expression.Constant(null)));
                }
            }

            // return expression for IsNull and IsNotNull
            if (value.FilterType == GridFilterType.IsNull)
            {
                value.FilterValue = "";
                if (targetType == typeof(string))
                {
                    return(GetExpression(binaryExpression, value, targetType, removeDiacritics));
                }
                else if (IsNullable)
                {
                    return(binaryExpression == null?
                           Expression.Equal(_expression.Body, Expression.Constant(null)) :
                               Expression.OrElse(Expression.Not(binaryExpression), Expression.Equal(expression, Expression.Constant(null))));
                }
                else
                {
                    return(binaryExpression == null ? null : Expression.Not(binaryExpression));
                }
            }
            else if (value.FilterType == GridFilterType.IsNotNull)
            {
                value.FilterValue = "";
                if (targetType == typeof(string))
                {
                    return(GetExpression(binaryExpression, value, targetType, removeDiacritics));
                }
                else if (IsNullable)
                {
                    return(binaryExpression == null?
                           Expression.NotEqual(_expression.Body, Expression.Constant(null)) :
                               Expression.AndAlso(binaryExpression, Expression.NotEqual(expression, Expression.Constant(null))));
                }
                else
                {
                    return(binaryExpression);
                }
            }
            else
            {
                return(GetExpression(binaryExpression, value, targetType, removeDiacritics));
            }
        }