private static Expression <Func <T, bool> > GetExpressionFromConditon <T> (string condition, IQueryObject queryObj, QueryColumnMapper <T> columnMapper) { try { string[] op = { "==", "!=", "=*", "!*", ">>", "<<", ">=", "<=" }; var maps = queryObj.ParseFilter(condition, op); if (!maps.HasValue) { return(null); } Expression <Func <T, object> > exp = columnMapper.Mappings[maps.Value.Left]; var body = exp.Body; // Remove the boxing for value types if (body.NodeType == ExpressionType.Convert) { body = ((UnaryExpression)body).Operand; } object value = maps.Value.Right; if (value != null && body.Type != value.GetType()) { var converter = TypeDescriptor.GetConverter(body.Type); value = converter.ConvertFromString(value.ToString()); // value = Convert.ChangeType(value, body.Type); } Expression be = null; var ContainsMethod = typeof(string).GetMethod("Contains", new [] { typeof(string) }); switch (maps.Value.Operation) { case "==": be = Expression.Equal(body, Expression.Constant(value, body.Type)); break; case "!=": be = Expression.NotEqual(body, Expression.Constant(value, body.Type)); break; case ">>": be = Expression.GreaterThan(body, Expression.Constant(value, body.Type)); break; case "<<": be = Expression.LessThan(body, Expression.Constant(value, body.Type)); break; case ">=": be = Expression.GreaterThanOrEqual(body, Expression.Constant(value, body.Type)); break; case "<=": be = Expression.LessThanOrEqual(body, Expression.Constant(value, body.Type)); break; case "=*": be = Expression.Call(body, ContainsMethod, Expression.Constant(value, body.Type)); break; case "!*": be = Expression.Not(Expression.Call(body, ContainsMethod, Expression.Constant(value, body.Type))); break; default: return(null); } return(Expression.Lambda <Func <T, bool> > (be, exp.Parameters)); } catch (Exception) { return(null); } }
private static Expression <Func <T, bool> > GetExpressionFromInternalConditions <T> (IQueryObject queryObj, QueryColumnMapper <T> columnMapper, List <(string condition, bool IsAnd)> conditions)