コード例 #1
0
        private static BinaryExpression GetExpression <T>
            (ParameterExpression param, FilterEngine filter1, FilterEngine filter2)
        {
            Expression bin1 = GetExpression <T>(param, filter1);
            Expression bin2 = GetExpression <T>(param, filter2);

            return(Expression.AndAlso(bin1, bin2));
        }
コード例 #2
0
        public FilterEngine CreateFilterByOp(Control controlType, string propertyName, Op operation, string value)
        {
            FilterEngine filter = null;

            switch (operation)
            {
            case Op.Equals:
            case Op.GreaterThan:
            case Op.GreaterThanOrEqual:
            case Op.LessThanOrEqual:
            case Op.LessThan:
            case Op.NotEquals:
                filter = new FilterEngine
                {
                    PropertyName = propertyName,
                    Operation    = operation
                };

                switch (controlType.GetType().Name)
                {
                case "TextBox":
                    filter.Value = Convert.ToInt32(value);
                    break;
                }
                break;

            case Op.Contains:
            case Op.StartsWith:
            case Op.EndsWith:

                filter = new FilterEngine
                {
                    PropertyName = propertyName,
                    Operation    = operation,
                    Value        = value
                };
                break;
            }
            return(filter);
        }
コード例 #3
0
        public FilterEngine GetFilterByPropertyType(PropertyInfo prop, string text)
        {
            FilterEngine filter = new FilterEngine {
                PropertyName = prop.Name.ToLower()
            };


            switch (prop.PropertyType.ToString())
            {
            case "System.Int32":

                filter.Operation = Op.Equals;

                if (text.IsDigitsOnly())
                {
                    filter.Value = Convert.ToInt32(text);
                }
                else
                {
                    filter.Value = -1;
                }

                break;

            case "System.String":

                filter.Operation = Op.Contains;
                filter.Value     = text.ToLower();

                break;


            case "global::System.DateTime":

                filter.Operation = Op.LessThanOrEqual;
                filter.Value     = text;
                break;

            case "System.Double":

                filter.Operation = Op.Equals;

                if (text.IsDigitsOnly())
                {
                    filter.Value = Convert.ToDouble(text);
                }
                else
                {
                    filter.Value = -1;
                }
                break;

            case "System.Nullable`1[System.Boolean]":
            case "System.Boolean":

                filter.Operation = Op.Equals;

                Boolean parsedValue;

                if (Boolean.TryParse(text.YesNoByBooleanValue(), out parsedValue))
                {
                    filter.Value = parsedValue;
                }
                else
                {
                    filter.Value = "false";
                }



                break;
            }
            return(filter);
        }
コード例 #4
0
        private static Expression GetExpression <T>(ParameterExpression param, FilterEngine filter)
        {
            var propertyName             = filter.PropertyName.ToLower();
            MemberExpression member      = Expression.Property(param, propertyName);
            string           filterValue = filter.Value.ToString().ToLower();

            UnaryExpression constant;

            if (member.Type == typeof(Int32?) || member.Type == typeof(Int32))
            {
                constant = Expression.Convert(Expression.Constant(Int32.Parse(filterValue)), member.Type);
            }
            else if (member.Type == typeof(DateTime?) || member.Type == typeof(DateTime))
            {
                constant = Expression.Convert(Expression.Constant(DateTime.Parse(filterValue)), member.Type);
            }
            else if (member.Type == typeof(Guid?) || member.Type == typeof(Guid))
            {
                constant = Expression.Convert(Expression.Constant(Guid.Parse(filterValue)), member.Type);
            }
            else if (member.Type == typeof(Boolean?) || member.Type == typeof(Boolean))
            {
                constant = Expression.Convert(Expression.Constant(Boolean.Parse(filterValue)), member.Type);
            }
            else
            {
                constant = Expression.Convert(Expression.Constant(filterValue), member.Type);
            }


            switch (filter.Operation)
            {
            case Op.Equals:
                if (member.Type.Name == "String")
                {
                    var expToLower = Expression.Call(member, ToLowerMethod);
                    return(Expression.Equal(expToLower, constant));
                }
                return(Expression.Equal(member, constant));

            case Op.GreaterThan:
                return(Expression.GreaterThan(member, constant));

            case Op.GreaterThanOrEqual:
                return(Expression.GreaterThanOrEqual(member, constant));

            case Op.LessThan:
                return(Expression.LessThan(member, constant));

            case Op.LessThanOrEqual:
                return(Expression.LessThanOrEqual(member, constant));

            case Op.Contains:

                var expToLowerContains = Expression.Call(member, ToLowerMethod);
                return(Expression.Call(expToLowerContains, ContainsMethod, constant));    // call StartsWith() on the exp, which is property.ToLower()

            case Op.StartsWith:

                var expToLowerStartsWith = Expression.Call(member, ToLowerMethod);
                return(Expression.Call(expToLowerStartsWith, StartsWithMethod, constant));

            case Op.EndsWith:

                var expToLowerEndsWith = Expression.Call(member, ToLowerMethod);
                return(Expression.Call(expToLowerEndsWith, EndsWithMethod, constant));

            case Op.NotEquals:
                if (member.Type.Name == "String")
                {
                    var expToLowerNotEqual = Expression.Call(member, ToLowerMethod);
                    return(Expression.NotEqual(expToLowerNotEqual, constant));
                }
                else
                {
                    return(Expression.NotEqual(member, constant));
                }
            }

            return(null);
        }