public static SqlPredicate Parse(Expression expression) { var methodCallExpression = expression as MethodCallExpression; if (methodCallExpression != null) { if (methodCallExpression.Method.Name == nameof(string.Contains)) { var memberExpression = methodCallExpression.Object as MemberExpression; return(QB.Like(memberExpression.Member.Name, Expression.Lambda(methodCallExpression.Arguments.First()).Compile().DynamicInvoke())); } else { throw new Exception($"Unsupported method {methodCallExpression.Method.Name} in expression {expression}."); } } var binaryExpression = expression as BinaryExpression; if (binaryExpression == null) { throw new Exception($"Unsupported expression {expression}."); } switch (binaryExpression.NodeType) { case ExpressionType.AndAlso: return(QB.And(Parse(binaryExpression.Left), Parse(binaryExpression.Right))); case ExpressionType.OrElse: return(QB.Or(Parse(binaryExpression.Left), Parse(binaryExpression.Right))); case ExpressionType.NotEqual: var nameAndValue0 = GetColumnNameAndParameterValue(binaryExpression); return(QB.NEq(nameAndValue0.Item1, nameAndValue0.Item2)); case ExpressionType.Equal: var nameAndValue1 = GetColumnNameAndParameterValue(binaryExpression); return(QB.Eq(nameAndValue1.Item1, nameAndValue1.Item2)); case ExpressionType.LessThan: var nameAndValue2 = GetColumnNameAndParameterValue(binaryExpression); return(QB.Le(nameAndValue2.Item1, nameAndValue2.Item2)); case ExpressionType.LessThanOrEqual: var nameAndValue3 = GetColumnNameAndParameterValue(binaryExpression); return(QB.LEq(nameAndValue3.Item1, nameAndValue3.Item2)); case ExpressionType.GreaterThan: var nameAndValue4 = GetColumnNameAndParameterValue(binaryExpression); return(QB.Gr(nameAndValue4.Item1, nameAndValue4.Item2)); case ExpressionType.GreaterThanOrEqual: var nameAndValue5 = GetColumnNameAndParameterValue(binaryExpression); return(QB.GEq(nameAndValue5.Item1, nameAndValue5.Item2)); default: throw new Exception($"Unsupported expression {expression}."); } }
public DbRepositoryQuery <T> Where(Expression <Func <T, bool> > whereExpression) { var newWhereStatement = Linq2PredicateParser.Parse(whereExpression.Body); QueryBuilder.SetWhereStatement(QueryBuilder.WhereStatement == null ? newWhereStatement : QB.And(QueryBuilder.WhereStatement, newWhereStatement)); return(this); }