public static SubstituteConstantsResultWithValues SubstituteConstants(Expression expression) { object[] args = null; SubstituteConstantsResult result; if (!cachedSubstitutedExpressions.TryGetValue(expression, out result)) { var values = new List <object>(); var parameters = new List <ParameterExpression>(); var replacement = SqlExpressionReplacer.Replace(expression, c => { if (c.NodeType != ExpressionType.Constant) { return(null); } var constantExpression = (ConstantExpression)c; values.Add(constantExpression.Value); var parameter = Expression.Parameter(constantExpression.Type); parameters.Add(parameter); return(parameter); }); args = values.ToArray(); var parametersArray = parameters.ToArray(); result = new SubstituteConstantsResult(replacement, parametersArray); cachedSubstitutedExpressions = cachedSubstitutedExpressions.Clone(expression, result, "cachedSubstitutedExpressions"); } if (args == null) { var i = 0; args = new object[result.AdditionalParameters.Length]; SqlExpressionReplacer.Replace(expression, c => { if (c.NodeType != ExpressionType.Constant) { return(null); } var constantExpression = (ConstantExpression)c; args[i++] = constantExpression.Value; return(null); }); } return(new SubstituteConstantsResultWithValues(result, args)); }
public static SubstituteConstantsResultWithValues SubstituteConstants(Expression expression) { object[] args = null; SubstituteConstantsResult result; if (!cachedSubstitutedExpressions.TryGetValue(expression, out result)) { var values = new List<object>(); var parameters = new List<ParameterExpression>(); var replacement = SqlExpressionReplacer.Replace(expression, c => { if (c.NodeType != ExpressionType.Constant) { return null; } var constantExpression = (ConstantExpression)c; values.Add(constantExpression.Value); var parameter = Expression.Parameter(constantExpression.Type); parameters.Add(parameter); return parameter; }); args = values.ToArray(); var parametersArray = parameters.ToArray(); result = new SubstituteConstantsResult(replacement, parametersArray); cachedSubstitutedExpressions = cachedSubstitutedExpressions.Clone(expression, result, "cachedSubstitutedExpressions"); } if (args == null) { var i = 0; args = new object[result.AdditionalParameters.Length]; SqlExpressionReplacer.Replace(expression, c => { if (c.NodeType != ExpressionType.Constant) { return null; } var constantExpression = (ConstantExpression)c; args[i++] = constantExpression.Value; return null; }); } return new SubstituteConstantsResultWithValues(result, args); }