/// <summary> /// Merge in the parameters from the incoming exp and renames them if there are any collisions. /// /// Returns a new instance of exp with the parameters replaced. /// </summary> /// <param name="exp"></param> /// <returns></returns> public ExpressionText MergeParameterMapping(ExpressionText exp) { // Para evitar que la expresión original quede manipulada la clonamos. // No usamos JsonClone clone por rendimiento // var expSource = ExtensionHelpers.JsonClone(exp); var expSource = new ExpressionText() { Parameters = new Dictionary <string, string>(exp.Parameters ?? new Dictionary <string, string>()), InternalExpression = exp.InternalExpression, Arguments = new Dictionary <string, object>(exp.Arguments ?? new Dictionary <string, object>()), ClrType = exp.ClrType }; if (exp.Parameters == null) { return(expSource); } this.Parameters = this.Parameters ?? new Dictionary <string, string>(); foreach (var key in expSource.Parameters.Keys.ToList()) { var keyName = this.FindFreeParameterPlaceholder(this, expSource, key); expSource.ReplaceParameterPlaceholder(key, keyName); this.Parameters[keyName] = expSource.Parameters[keyName]; } return(expSource); }
/// <summary> /// Transform a lambda expression into it's text representation /// </summary> /// <param name="expression"></param> /// <returns></returns> public ExpressionText GetExpressionText( Expression expression) { this.Arguments = new Dictionary <string, object>(); this.Parameters = new Dictionary <string, string>(); this.TempParameters = new Dictionary <string, ParameterExpression>(); ExpressionText result = new ExpressionText(); var exp = this.VisitExpression(expression, null); result.InternalExpression = exp.Text; result.Arguments = this.Arguments; result.Parameters = this.Parameters; result.Parameters2 = this.TempParameters; result.ClrType = exp.ClrType?.FullName; return(result); }
/// <summary> /// /// </summary> /// <param name="exp1"></param> /// <param name="exp2"></param> /// <param name="proposal"></param> /// <param name="freeIfMatchingValue"></param> /// <returns></returns> public string FindFreeParameterPlaceholder( ExpressionText exp1, ExpressionText exp2, string proposal, bool freeIfMatchingValue = true) { int x = 0; string key = $"{proposal}"; if (exp1.Parameters == null || !exp1.Parameters.ContainsKey(key)) { return(key); } while (true) { // Si la proyección es igual, podemos sobrescribir if (freeIfMatchingValue && exp1.Parameters.ContainsKey(key) && exp2.Parameters.ContainsKey(key) && exp1.Parameters[key] == exp2.Parameters[key]) { return(key); } if (exp1.Parameters == null || exp2.Parameters == null) { return(key); } if (!exp1.Parameters.ContainsKey(key) && !exp2.Parameters.ContainsKey(key)) { return(key); } key = $"{proposal}_{x}"; x++; } }