コード例 #1
0
        private Expression <Func <T, bool> > GetFilterExpression(PropertyInfo pi, ColumnFilterValue value)
        {
            //detect nullable
            bool isNullable = pi.PropertyType.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;

            if (value.FilterValue.Contains("|"))
            {
                var filterValues = value.FilterValue.Split('|');
                binaryExpression = filterType.GetFilterExpression(firstExpr, filterValues[0], value.FilterType);

                for (int i = 1; i < filterValues.Count(); i++)
                {
                    Expression tempExpr = filterType.GetFilterExpression(firstExpr, filterValues[i], value.FilterType);
                    binaryExpression = Expression.Or(tempExpr, binaryExpression);
                }
            }
            else
            {
                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));
        }
コード例 #2
0
        public override bool Equals(object obj)
        {
            if (!(obj is ColumnFilterValue))
            {
                return(false);
            }

            ColumnFilterValue compareObject = (ColumnFilterValue)obj;

            return(compareObject.ColumnName == ColumnName &&
                   compareObject.FilterType == FilterType &&
                   compareObject.FilterValue == FilterValue);
        }
コード例 #3
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));
        }
コード例 #4
0
        public QueryStringFilterSettings(HttpContext context)
        {
            if (context == null)
            {
                throw new ArgumentException("No http context here!");
            }
            Context = context;

            string[] filters = Context.Request.QueryString.GetValues(DefaultTypeQueryParameter);
            if (filters != null)
            {
                foreach (string filter in filters)
                {
                    ColumnFilterValue column = CreateColumnData(filter);
                    if (column != ColumnFilterValue.Null)
                    {
                        _filterValues.Add(column);
                    }
                }
            }
        }
コード例 #5
0
        public QueryStringFilterSettings(IQueryDictionary <StringValues> query)
        {
            if (query == null)
            {
                throw new ArgumentException("No http context here!");
            }
            Query = query;

            var filters = Query.Get(DefaultTypeQueryParameter);

            if (filters.Count > 0)
            {
                foreach (string filter in filters)
                {
                    ColumnFilterValue column = CreateColumnData(filter);
                    if (column != ColumnFilterValue.Null)
                    {
                        _filterValues.Add(column);
                    }
                }
            }
        }
コード例 #6
0
        public QueryStringFilterSettings(IQueryCollection query)
        {
            if (query == null)
            {
                throw new ArgumentException("No http context here!");
            }
            Query = query;

            string[] filters = Query[DefaultTypeQueryParameter].Count > 0 ?
                               Query[DefaultTypeQueryParameter].ToArray() : null;
            if (filters != null)
            {
                foreach (string filter in filters)
                {
                    ColumnFilterValue column = CreateColumnData(filter);
                    if (column != ColumnFilterValue.Null)
                    {
                        _filterValues.Add(column);
                    }
                }
            }
        }