private static SqlExpression VisitMethodCall(MethodCallExpression methodCallExpression) { if (methodCallExpression.Method.DeclaringType.Name == "String") { return new SqlWhereClause { Left = Visit(methodCallExpression.Object), Operator = SqlOperator.Like, Right = VisitLike(Visit(methodCallExpression.Arguments[0]), methodCallExpression) }; } if (methodCallExpression.Method.Name == "Contains") { var arg = ((SqlWhereConstant)Visit(methodCallExpression.Arguments[0])).Value; var column = (MemberExpression)methodCallExpression.Arguments[1]; var left = new SqlWhereColumn { Prefix = ((ParameterExpression)column.Expression).Name, Name = QueryTranslator.GetColumnName(column.Member) }; var right = new SqlWhereConstant { Value = arg }; var opera = SqlOperator.In; return new SqlWhereClause { Left = left, Operator = opera, Right = right }; } if (Type.GetTypeCode(methodCallExpression.Method.ReturnType) != TypeCode.Object) { var objExpression = Visit(methodCallExpression.Object); if (objExpression is SqlWhereConstant) { var args = GetArgumentsValue(methodCallExpression.Arguments); return new SqlWhereConstant { Value = methodCallExpression.Method.Invoke(((SqlWhereConstant)objExpression).Value, args) }; } } return null; }
private static SqlExpression VisitMemberAccess(MemberExpression memberExpression) { if (memberExpression.Expression != null && memberExpression.Expression.NodeType == ExpressionType.Parameter) { var column = new SqlWhereColumn { Prefix = ((ParameterExpression)memberExpression.Expression).Name, Name = QueryTranslator.GetColumnName(memberExpression.Member) }; return column; } else if (memberExpression.Expression != null && memberExpression.Expression is MemberExpression && ((MemberExpression)memberExpression.Expression).Expression.NodeType == ExpressionType.Parameter) { var sql = new SqlWhereColumn { Prefix = ((MemberExpression)memberExpression.Expression).Member.Name, Name = QueryTranslator.GetColumnName(memberExpression.Member) }; return sql; } else if (memberExpression.Expression != null && memberExpression.Expression is MemberExpression && ((MemberExpression)memberExpression.Expression).Expression.NodeType == ExpressionType.Constant) { var lambda = Expression.Lambda(memberExpression); var fn = lambda.Compile(); return Visit(Expression.Constant(fn.DynamicInvoke(null), memberExpression.Type)); } else if (memberExpression.Expression != null && memberExpression.Expression is MemberExpression && !(((MemberExpression)memberExpression.Expression).Expression.NodeType == ExpressionType.MemberAccess)) { var sql = new SqlWhereColumn { Prefix = ((MemberExpression)memberExpression.Expression).Member.Name, Name = QueryTranslator.GetColumnName(memberExpression.Member) }; return sql; } else { var lambda = Expression.Lambda(memberExpression); var fn = lambda.Compile(); return Visit(Expression.Constant(fn.DynamicInvoke(null), memberExpression.Type)); } }