public static IEnumerable <string> GetOperators(FilterOperations op) { switch (op) { case FilterOperations.Comparition: return(new string[] { "<", "<=", "=", "!=", ">", ">=" }); case FilterOperations.List: return(new string[] { "in", "not in" }); default: throw new NotSupportedException($"The operation '{op.ToString()}' is not supported and does not have any operators."); } }
/// <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); }