private static Expression ArrayContainsValue( Expression propExpr, object propValue) { var propType = propExpr.Type; Expression expr = null; if (propValue as object[] == null) { var valueExpr = ValueToExpression(propValue, propType); expr = ExpressionEx.ArrayContains(propExpr, valueExpr); } else { foreach (var val in propValue as object[]) { var valueExpr = ValueToExpression(val, propType); var itemExpr = ExpressionEx.ArrayContains(propExpr, valueExpr); if (expr == null) { expr = itemExpr; } else { expr = Expression.Or(expr, itemExpr); } } } return(expr); }
/// <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); }
/// <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); }