CreateSearchPredicate <T>(ExpressionOpFactory factory, GridOptions options) { bool isGroupOperationAND = options.Filters.GroupOperation == GridGroupSearchOperation.AND; var predicate = isGroupOperationAND ? PredicateBuilder.True <T>() : PredicateBuilder.False <T>(); foreach (var rule in options.Filters.FilterRules) { var operationAttribute = factory.GetExpressionType(rule.Operation); PropertyInfo pi = typeof(T).GetProperty(rule.Field); ParameterExpression lhsParam = Expression.Parameter(typeof(T)); Expression lhs = Expression.Property(lhsParam, pi); Expression rhs = Expression.Constant(Convert.ChangeType(rule.FieldData, pi.PropertyType)); Expression theOperation; if (operationAttribute.UseMethod) { lhs = Expression.Call(lhs, operationAttribute.Method, null, rhs); } if (operationAttribute.IsBinary) { theOperation = Expression.MakeBinary(operationAttribute.ExpressionType, lhs, rhs); } else //TODO: need to fix this { theOperation = Expression.MakeBinary(operationAttribute.ExpressionType, lhs, Expression.Constant(operationAttribute.MethodResultCompareValue)); } var theLambda = Expression.Lambda <Func <T, bool> >(theOperation, lhsParam); if (isGroupOperationAND) { predicate = predicate.And(theLambda); } else { predicate = predicate.Or(theLambda); } } return(predicate); }
public GridController() { _connectionString = "mongodb://localhost:27020"; _dbName = "Baseball2013"; _config = new OpConfig() .Add("eq", new ExpressionBehavior { IsBinary = true, ExpressionType = ExpressionType.Equal }) .Add("ne", new ExpressionBehavior { IsBinary = true, ExpressionType = ExpressionType.NotEqual }) .Add("gt", new ExpressionBehavior { IsBinary = true, ExpressionType = ExpressionType.GreaterThan }) .Add("ge", new ExpressionBehavior { IsBinary = true, ExpressionType = ExpressionType.GreaterThanOrEqual }) .Add("lt", new ExpressionBehavior { IsBinary = true, ExpressionType = ExpressionType.LessThan }) .Add("le", new ExpressionBehavior { IsBinary = true, ExpressionType = ExpressionType.LessThanOrEqual }) .Add("bw", new ExpressionBehavior { IsBinary = false, MethodResultCompareValue = true, ExpressionType = ExpressionType.Equal, UseMethod = true, Method = "StartsWith" }) .Add("bn", new ExpressionBehavior { IsBinary = false, MethodResultCompareValue = false, ExpressionType = ExpressionType.Equal, UseMethod = true, Method = "StartsWith" }) .Add("ew", new ExpressionBehavior { IsBinary = false, MethodResultCompareValue = true, ExpressionType = ExpressionType.Equal, UseMethod = true, Method = "EndsWith" }) .Add("en", new ExpressionBehavior { IsBinary = false, MethodResultCompareValue = false, ExpressionType = ExpressionType.Equal, UseMethod = true, Method = "EndsWith" }) .Add("cn", new ExpressionBehavior { IsBinary = false, MethodResultCompareValue = true, ExpressionType = ExpressionType.Equal, UseMethod = true, Method = "Contains" }) .Add("nc", new ExpressionBehavior { IsBinary = false, MethodResultCompareValue = false, ExpressionType = ExpressionType.Equal, UseMethod = true, Method = "Contains" }); _factory = new ExpressionOpFactory(_config); }