コード例 #1
0
        internal GenericSpecification <T> Match <TArg>(string property, GenericSearchFilter filter, TArg value)
        {
            if (string.IsNullOrEmpty(property))
            {
                throw new ArgumentException("property can't be empty");
            }
            MemoryStream arg = value != null?Protobuf.Serialize(value) : null;

            Filters.Add(property, new KeyValuePair <int, MemoryStream>((int)filter, arg));
            return(this);
        }
コード例 #2
0
        public GenericSpecification <T, TFormat> Match <TArg>(string property, GenericSearchFilter filter, TArg value)
        {
            if (string.IsNullOrEmpty(property))
            {
                throw new ArgumentException("property can't be empty");
            }
            specificationFilter = null;
            List <KeyValuePair <int, TFormat> > list;

            if (!Filters.TryGetValue(property, out list))
            {
                Filters[property] = list = new List <KeyValuePair <int, TFormat> >();
            }
            list.Add(new KeyValuePair <int, TFormat>((int)filter, Serializer.Serialize(value)));
            return(this);
        }
コード例 #3
0
        private Expression Filter(
            ParameterExpression arg,
            string path,
            GenericSearchFilter filter,
            TFormat value)
        {
            var props = path.Split('.');
            var type  = typeof(T);

            Expression expr = arg;

            foreach (var prop in props)
            {
                // use reflection (not ComponentModel) to mirror LINQ
                var pi = type.GetProperty(prop);
                if (pi == null)
                {
                    var msg = string.Format(CultureInfo.InvariantCulture, "Unknown property: {0} on type {1}", prop, type.FullName);
                    if (prop != path)
                    {
                        msg += " for path " + path;
                    }
                    throw new ArgumentException(msg);
                }
                expr = Expression.Property(expr, pi);
            }
            switch (filter)
            {
            case GenericSearchFilter.Equals:
                return(Expression.Equal(expr, Expression.Constant(Deserialize(expr.Type, value))));

            case GenericSearchFilter.NotEquals:
                return(Expression.NotEqual(expr, Expression.Constant(Deserialize(expr.Type, value))));

            case GenericSearchFilter.LessThen:
                return(Expression.LessThan(expr, Expression.Constant(Deserialize(expr.Type, value))));

            case GenericSearchFilter.LessOrEqualThen:
                return(Expression.LessThanOrEqual(expr, Expression.Constant(Deserialize(expr.Type, value))));

            case GenericSearchFilter.GreaterThen:
                return(Expression.GreaterThan(expr, Expression.Constant(Deserialize(expr.Type, value))));

            case GenericSearchFilter.GreaterThenOrEqual:
                return(Expression.GreaterThanOrEqual(expr, Expression.Constant(Deserialize(expr.Type, value))));

            case GenericSearchFilter.InValue:
                return(Expression.Call(
                           ContainsMethod.MakeGenericMethod(expr.Type),
                           Expression.Constant(Deserialize(expr.Type.MakeArrayType(), value)),
                           expr));

            case GenericSearchFilter.NotInValue:
                return(Expression.Not(
                           Expression.Call(ContainsMethod.MakeGenericMethod(expr.Type),
                                           Expression.Constant(Deserialize(expr.Type, value)),
                                           expr)));

            case GenericSearchFilter.ValueIn:
                return(Expression.Call(
                           ContainsMethod.MakeGenericMethod(expr.Type),
                           expr,
                           Expression.Constant(Deserialize(expr.Type.GetElementType(), value))));

            case GenericSearchFilter.ValueNotIn:
                return(Expression.Not(
                           Expression.Call(
                               ContainsMethod.MakeGenericMethod(expr.Type),
                               expr,
                               Expression.Constant(Deserialize(expr.Type.GetElementType(), value)))));

            case GenericSearchFilter.StartsWithValue:
                return(Expression.Call(expr, StringStartsWith, Expression.Constant(Deserialize(expr.Type, value))));

            case GenericSearchFilter.StartsWithCaseInsensitiveValue:
                return(Expression.Call(
                           expr,
                           StringStartsWithCaseInsensitive,
                           Expression.Constant(Deserialize(expr.Type, value)),
                           Expression.Constant(StringComparison.InvariantCultureIgnoreCase)));

            case GenericSearchFilter.NotStartsWithValue:
                return(Expression.Not(
                           Expression.Call(
                               expr,
                               StringStartsWith,
                               Expression.Constant(Deserialize(expr.Type, value)))));

            case GenericSearchFilter.NotStartsWithCaseInsensitiveValue:
                return(Expression.Not(
                           Expression.Call(
                               expr,
                               StringStartsWithCaseInsensitive,
                               Expression.Constant(Deserialize(expr.Type, value)),
                               Expression.Constant(StringComparison.InvariantCultureIgnoreCase))));

            case GenericSearchFilter.ValueStartsWith:
                return(Expression.Call(
                           Expression.Constant(Deserialize(expr.Type, value)),
                           StringStartsWith,
                           expr));

            case GenericSearchFilter.ValueStartsWithCaseInsensitive:
                return(Expression.Call(
                           Expression.Constant(Deserialize(expr.Type, value)),
                           StringStartsWithCaseInsensitive,
                           expr,
                           Expression.Constant(StringComparison.InvariantCultureIgnoreCase)));

            case GenericSearchFilter.NotValueStartsWith:
                return(Expression.Not(
                           Expression.Call(
                               Expression.Constant(Deserialize(expr.Type, value)),
                               StringStartsWith,
                               expr)));

            case GenericSearchFilter.NotValueStartsWithCaseInsensitive:
                return(Expression.Not(
                           Expression.Call(
                               Expression.Constant(Deserialize(expr.Type, value)),
                               StringStartsWithCaseInsensitive,
                               expr,
                               Expression.Constant(StringComparison.InvariantCultureIgnoreCase))));

            default:
                throw new ArgumentException("Unknown filter");
            }
        }