Ejemplo n.º 1
0
        /// <summary>
        /// MakePropertyValueComparison
        /// </summary>
        /// <returns>The property value comparison.</returns>
        /// <param name="propExpr">Property expr.</param>
        /// <param name="filterOperator">Filter operator.</param>
        /// <param name="propValue">Property value.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public static Expression MakePropertyValueComparison(Expression propExpr, FilterOperations filterOperator, object propValue)
        {
            Type propType = propExpr.Type;

            Expression valueExpr = Expression.Constant(propValue);
            Expression expr;

            if (filterOperator != FilterOperations.Ct && filterOperator != FilterOperations.Nct)
            {
                valueExpr = ValueToExpression(propValue, propType);
            }
            else if (propValue as object[] != null)
            {
                valueExpr = ArrayValueToExpression(propValue as object[], propType);
                expr      = ExpressionEx.ArrayContains(propExpr, valueExpr);
                return(expr);
            }
            switch (filterOperator)
            {
            case FilterOperations.Eq:
                expr = Expression.Equal(propExpr, valueExpr);
                break;

            case FilterOperations.Neq:
                expr = Expression.NotEqual(propExpr, valueExpr);
                break;

            case FilterOperations.Ct:
                expr = ExpressionEx.Contains(propExpr, valueExpr);
                break;

            case FilterOperations.Nct:
                expr = Expression.Not(ExpressionEx.Contains(propExpr, valueExpr));
                break;

            case FilterOperations.Sw:
                expr = ExpressionEx.StartsWith(propExpr, valueExpr);
                break;

            case FilterOperations.Ew:
                expr = ExpressionEx.EndsWith(propExpr, valueExpr);
                break;

            case FilterOperations.Gt:
                expr = Expression.GreaterThan(propExpr, valueExpr);
                break;

            case FilterOperations.Gte:
                expr = Expression.GreaterThanOrEqual(propExpr, valueExpr);
                break;

            case FilterOperations.Lt:
                expr = Expression.LessThan(propExpr, valueExpr);
                break;

            case FilterOperations.Lte:
                expr = Expression.LessThanOrEqual(propExpr, valueExpr);
                break;

            default:
                throw new NotSupportedException(
                          string.Format("Filter operator '{0}' is not supported yet.",
                                        filterOperator.ToString()));
            }
            return(expr);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// MakeDictionayValueComparison
        /// </summary>
        /// <returns>The dictionay value comparison.</returns>
        /// <param name="propExpr">Property expr.</param>
        /// <param name="filterOperator">Filter operator.</param>
        /// <param name="propValue">Property value.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        private static Expression MakeDictionayValueComparison(Expression propExpr, FilterOperations filterOperator, object propValue)
        {
            Expression valueExpr = Expression.Constant(propValue);
            Expression expr;

            if (propValue == null)
            {
                return(filterOperator == FilterOperations.Eq ? Expression.Equal(propExpr, valueExpr) : Expression.NotEqual(propExpr, valueExpr));
            }

            string val      = propValue.ToString();
            Type   propType = propExpr.Type;

            if (propValue is Array)
            {
                val = (propValue as Array).GetValue(0).ToString();
                if (val == string.Empty && ((Array)propValue).Length > 1)
                {
                    val = (propValue as Array).GetValue(1).ToString();
                }
            }

            if (propType == typeof(string) && filterOperator == FilterOperations.Eq)
            {
                filterOperator = FilterOperations.Ct;
            }

            if (filterOperator != FilterOperations.Ct && filterOperator != FilterOperations.Nct)
            {
                // Creates (x.Value as propType)
                valueExpr = ValueToExpression(propValue, propType);
            }
            else
            {
                if (propValue as object[] != null)
                {
                    valueExpr = ArrayValueToExpression(propValue as object[], propType);
                    return(ExpressionEx.ArrayContains(propExpr, valueExpr));
                }
            }

            switch (filterOperator)
            {
            case FilterOperations.Eq:
                expr = Expression.Equal(propExpr, valueExpr);
                break;

            case FilterOperations.Neq:
                expr = Expression.NotEqual(propExpr, valueExpr);
                break;

            case FilterOperations.Ct:
                expr = ExpressionEx.Contains(propExpr, valueExpr);
                break;

            case FilterOperations.Nct:
                expr = Expression.Not(ExpressionEx.Contains(propExpr, valueExpr));
                break;

            case FilterOperations.Sw:
                expr = ExpressionEx.StartsWith(propExpr, valueExpr);
                break;

            case FilterOperations.Ew:
                expr = ExpressionEx.EndsWith(propExpr, valueExpr);
                break;

            case FilterOperations.Gt:
                expr = Expression.GreaterThan(propExpr, valueExpr);
                break;

            case FilterOperations.Gte:
                expr = Expression.GreaterThanOrEqual(propExpr, valueExpr);
                break;

            case FilterOperations.Lt:
                expr = Expression.LessThan(propExpr, valueExpr);
                break;

            case FilterOperations.Lte:
                expr = Expression.LessThanOrEqual(propExpr, valueExpr);
                break;

            default:
                throw new NotSupportedException(
                          string.Format("Filter operator '{0}' is not supported yet.",
                                        filterOperator.ToString()));
            }

            return(expr);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Find records with string Fields (if not specified, will filter all string Fields on the object) has contains the value.
        /// </summary>
        /// <returns>The records with data field value after today.</returns>
        /// <param name="fields">Fields name array to specify which field to filter</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        private static Expression StringContainsValue(ParameterExpression source, object value, string[] fields = null, bool caseSensitive = false)
        {
            var type = source.Type;

            if (fields == null || fields.Length == 0)
            {
                fields = type.GetProperties()
                         .Where(p => p.PropertyType == typeof(string))
                         .Select(p => p.Name).ToArray();
            }

            var        valueExpr        = Expression.Constant(value);
            Expression selectLeft       = null;
            Expression selectRight      = null;
            Expression filterExpression = null;

            foreach (var field in fields)
            {
                Expression propExpr   = source;
                var        members    = field.Split('.');
                Expression comparison = null;

                Expression propNotNull = null;
                foreach (var member in members)
                {
                    //navigation property
                    propExpr = Expression.PropertyOrField(propExpr, member);

                    var t = propExpr.Type;
                    if (members.Length > 1)
                    {
                        var pNotNull = Expression.NotEqual(propExpr, Expression.Constant(null));
                        if (propNotNull == null)
                        {
                            propNotNull = pNotNull;
                        }
                        else
                        {
                            propNotNull = Expression.AndAlso(propNotNull, pNotNull);
                        }
                    }
                    if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IDictionary <,>))
                    {
                        // remove prop type info, treat all item as string item
                        comparison = DictionaryContainsItem(propExpr, members.Last(), FilterOperations.Ct, value);
                        comparison = Expression.AndAlso(propNotNull, comparison);
                        break;
                    }
                }

                if (propExpr.Type == typeof(string))
                {
                    comparison = ExpressionEx.Contains(propExpr, valueExpr, caseSensitive);
                }

                if (comparison == null)
                {
                    continue;
                }

                if (selectLeft == null)
                {
                    selectLeft       = comparison;
                    filterExpression = selectLeft;
                    continue;
                }
                if (selectRight == null)
                {
                    selectRight      = comparison;
                    filterExpression =
                        Expression.Or(selectLeft, selectRight);
                    continue;
                }
                filterExpression =
                    Expression.Or(filterExpression, comparison);
            }
            return(filterExpression);
        }