-
-
Notifications
You must be signed in to change notification settings - Fork 231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: How to keep parameter input name of query #119
Comments
This is a workaround, but it only helps if you know the input parameter name..
Is there a way to extract only the input parameter name from a expression string? |
I see your point. I've to check if this can be fixed. This issue is maybe a bit related to #110 ? |
Yes, this looks like to have the same solution. |
I have the same Problem and as far as I can see, it comes from the calls to
Instead of giving an arbitrary name to the Parser, it should be the other way around, so the parser determines the name given the type of the parameter. |
The main question is : Why do you need this? |
I'm getting the string as an MVC Controller parameter. When debugging it is way more readable if you have the original parameter name. If you want to go from string to expression and back to string, you should be able to get your original string back. Currently this is not possible. |
@wertzui and @Franki1986 Code is like: public class Renamer : ExpressionVisitor
{
private readonly string _newName;
public Renamer(string newName)
{
_newName = newName;
}
public Expression Rename(Expression expression)
{
return Visit(expression);
}
protected override Expression VisitParameter(ParameterExpression node)
{
if (string.IsNullOrEmpty(node.Name))
{
return Expression.Parameter(node.Type, _newName);
}
else
{
return node;
}
}
} And can be used like: void Main()
{
var expression = DynamicExpressionParser.ParseLambda<string, bool>(new ParsingConfig { }, true, "s => s.Length == 8");
expression.ToString().Dump();
var newExp = new Renamer("something").Rename(expression);
newExp.ToString().Dump();
"---".Dump();
var expression1 = DynamicExpressionParser.ParseLambda<TestObject, bool>(new ParsingConfig { }, true, "dto => dto.X == 8");
expression1.ToString().Dump();
var newExp1 = new Renamer("dto").Rename(expression1);
newExp1.ToString().Dump();
}
public class TestObject
{
public int X { get; set; }
public string Name { get; set; }
} Output is:
The only thing what needs to be done is that the |
For that to work I would neet to know the name of the Parameter upfront. But I do not know the name because I get the expression string from the consumers of my API. I could probably work out the parameter name, but if I do not want to relay on error prone string inspection, I would have to parse the expression myself. And that is exactly what the Expressionparser does best ;) |
@wertzui and @Franki1986 I've build a solution, use this NuGet from a MyGet-feed: What you need to do is: var config = new ParsingConfig
{
RenameParameterExpression = true
};
var exp = DynamicExpressionParser.ParseLambda<string, bool>(config, true, "s => s.Length == 7");
Console.WriteLine(exp.ToString()); Can you please try this? |
@wertzui and @Franki1986 can you please try? |
Sorry for delay. On monday I am back to office, then I will test it. Thanks for your work! |
Mhh.. in the myget package you provided there is no 'RenameParameterExpression' property... |
Sorry. |
Ok, tried to get the package over myget, don't know if there is a problem with the source, but the current version is still ci-1369. Is there a way to get the specific version? |
Downloaded the latest master folder of 'System.Linq.Dynamic.Core' and tested ist this way. |
or <PackageReference Include="System.Linq.Dynamic.Core" Version="1.0.9.1-ci-1468" /> Thanks for testing. I'll close this issue now. |
I want to parse a string to a LambdaExpression.
Following classes:
And the query:
var expressionString = "pe => (( pe.Name == \"Hallo\" ))";
Now if I parse the expression
var expression = DynamicExpressionParser.ParseLambda(typeof(Person), null, expressionString);
The 'pe' name for my input parameter is lost or replaced.
Is there a way to preserve the given name?
The text was updated successfully, but these errors were encountered: