Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StringFilter"/> class.
        /// </summary>
        /// <param name="propertyInfo">The property info.</param>
        /// <param name="filterMode">The filter mode.</param>
        public StringFilter(ItemPropertyInfo propertyInfo, StringFilterMode filterMode = StringFilterMode.Contains)
            : base()
        {
            //if (!typeof(string).IsAssignableFrom(propertyInfo.PropertyType))
            //    throw new ArgumentOutOfRangeException("propertyInfo", "typeof(string).IsAssignableFrom(propertyInfo.PropertyType) return False.");
            Debug.Assert(propertyInfo != null, "propertyInfo is null.");
            Debug.Assert(typeof(IComparable).IsAssignableFrom(propertyInfo.PropertyType), "The typeof(IComparable).IsAssignableFrom(propertyInfo.PropertyType) return False.");
            base.PropertyInfo = propertyInfo;
            _filterMode       = filterMode;
            Func <object, object> getterItem = ((PropertyDescriptor)(PropertyInfo.Descriptor)).GetValue;

            this.getter = t => ((string)(getterItem(t)));
            base.Name   = "String";
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StringFilter"/> class.
        /// </summary>
        /// <param name="propertyInfo">The property info.</param>
        /// <param name="filterMode">The filter mode.</param>
        public StringFilter(ItemPropertyInfo propertyInfo, StringFilterMode filterMode = StringFilterMode.Contains)
            : base(((PropertyDescriptor)(propertyInfo.Descriptor)).GetValue)
        {
            //if (!typeof(string).IsAssignableFrom(propertyInfo.PropertyType))
            //    throw new ArgumentOutOfRangeException("propertyInfo", "typeof(string).IsAssignableFrom(propertyInfo.PropertyType) return False.");
            //Debug.Assert(propertyInfo != null, "propertyInfo is null.");
#if DEBUG
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue(typeof(IComparable).IsAssignableFrom(propertyInfo.PropertyType));
#endif
            //base.PropertyInfo = propertyInfo;
            _filterMode = filterMode;
            //Func<object, object> getterItem = ((PropertyDescriptor)(PropertyInfo.Descriptor)).GetValue;
            //this.getter = t => ((string)(getterItem(t)));
            base.name = "String";
        }
Пример #3
0
        private static Expression FilterString(
            this IQueryable queryable,
            ParameterExpression sharedParameterExpression,
            string propertyName,
            string[] values,
            StringFilterMode stringFilterMode)
        {
            if (queryable == null)
            {
                throw new ArgumentNullException("queryable");
            }

            if ((values == null) || (values.Length == 0))
            {
                throw new ArgumentNullException("values");
            }

            string methodName = QueryableExtensions.GetMethodNameFromStringFilterMode(stringFilterMode);

            Debug.Assert(!String.IsNullOrEmpty(methodName));

            MemberExpression memberExpression = QueryableExtensions.GenerateMemberExpression(sharedParameterExpression, propertyName);

            Expression mergedFilterCall = null;

            for (int i = 0; i < values.Length; i++)
            {
                ConstantExpression valueExpression = Expression.Constant(values[i]);

                MethodCallExpression newFilterCall = Expression.Call(
                    memberExpression,
                    typeof(string).GetMethod(methodName, new Type[] { typeof(string) }),
                    Expression.Convert(valueExpression, memberExpression.Type));

                if (mergedFilterCall == null)
                {
                    mergedFilterCall = newFilterCall;
                }
                else
                {
                    mergedFilterCall = MethodCallExpression.Or(mergedFilterCall, newFilterCall);
                }
            }

            return(mergedFilterCall);
        }
Пример #4
0
        public StringFilterPredicate(StringFilterMode mode             = StringFilterMode.StartsWith,
                                     bool isCaseSensitive              = false, GetItemTextCallback getItemText = null,
                                     NeedsRefreshCallback needsRefresh = null)
        {
            _mode            = mode;
            _isCaseSensitive = isCaseSensitive;
            _getItemText     = getItemText;
            _needsRefresh    = needsRefresh;

            if (_getItemText == null)
            {
                // by default we use item ToString()
                _getItemText = (item) => { return(item.ToString()); };
            }

            if (_needsRefresh == null)
            {
                // by default changes in item don't affect this predicate
                _needsRefresh = (item, propertyName) => { return(false); };
            }
        }
Пример #5
0
        private static string GetMethodNameFromStringFilterMode(StringFilterMode stringFilterMode)
        {
            switch (stringFilterMode)
            {
            case StringFilterMode.StartsWith:
            {
                return("StartsWith");
            }

            case StringFilterMode.Contains:
            {
                return("Contains");
            }

            case StringFilterMode.EndsWith:
            {
                return("EndsWith");
            }
            }

            Debug.Fail("Should have handled the new StringFilterMode.");
            return(string.Empty);
        }
Пример #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StringFilter"/> class.
        /// </summary>
        /// <param name="propertyInfo">The property info.</param>
        /// <param name="filterMode">The filter mode.</param>
        public StringFilter(ItemPropertyInfo propertyInfo, StringFilterMode filterMode = StringFilterMode.Contains)
            : base()
        {
            // if (!typeof(string).IsAssignableFrom(propertyInfo.PropertyType))
            //    throw new ArgumentOutOfRangeException("propertyInfo", "typeof(string).IsAssignableFrom(propertyInfo.PropertyType) return False.");
            Debug.Assert(propertyInfo != null, "propertyInfo is null.");
            // Debug.Assert(typeof(IComparable).IsAssignableFrom(propertyInfo.PropertyType), "The typeof(IComparable).IsAssignableFrom(propertyInfo.PropertyType) return False.");
            base.PropertyInfo = propertyInfo;
            this.filterMode   = filterMode;
            Func <object, object> getterItem = (o) =>
            {
                var d      = this.PropertyInfo.Descriptor;
                var pd     = (PropertyDescriptor)d;
                var result = pd.GetValue(o);
                return(result);
            };

            this.getter = t =>
            {
                var result = getterItem(t);
                return(result?.ToString());
            };
            base.Name = Resources.Strings.TextFilterText;
        }
        public static IQueryable <T> FilterOptionalString <T>(this IQueryable <T> data, Expression <Func <T, string> > fieldSelector, string valueToFilter, StringFilterMode mode)
        {
            if (string.IsNullOrEmpty(valueToFilter))
            {
                return(data);
            }

            Expression body;

            switch (mode)
            {
            case StringFilterMode.Contains:
                body = Expression.Call(fieldSelector.Body, containsMethod, Expression.Constant(valueToFilter));
                break;

            case StringFilterMode.StartsWith:
                body = Expression.Call(fieldSelector.Body, startsWithMethod, Expression.Constant(valueToFilter));
                break;

            case StringFilterMode.Equals:
                body = Expression.Equal(fieldSelector.Body, Expression.Constant(valueToFilter));
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(mode), mode, null);
            }
            return(data.Where(Expression.Lambda <Func <T, bool> >(body, fieldSelector.Parameters)));
        }
Пример #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StringFilter"/> class.
 /// </summary>
 /// <param name="propertyInfo">The property info.</param>
 /// <param name="filterMode">The filter mode.</param>
 /// <param name="value">The value.</param>
 public StringFilter(ItemPropertyInfo propertyInfo, StringFilterMode filterMode, string value)
     : this(propertyInfo, filterMode)
 {
     _value        = value;
     base.IsActive = !String.IsNullOrEmpty(value);
 }