Extract input parameter name from lambda expression

dynamic-linq expression-trees linq linq-to-nhibernate

Question

I am working with system.linq.dynamic.core and nhibernate linq. Now I want to pass two dynamic linq expression to a method. One for the parent table and one for a joined child list. The input parameter for both expressions are random, so an expression could be

"x => x.Name == \"Wick\" "

Or

" k => k.FirstName = \"John\" "

Now I know that with dynamic linq you could parse the string expressions to a LambdaExpression.

If I want to create a join there has to be an alias for the joined table. So from the expression I would like to extract the 'x' or the 'k'. Is this possible?

1
1
11/13/2017 7:11:30 PM

Accepted Answer

LambdaExpression has a Parameters property, which you can use to access information about lambda's parameters. In your case, that could be something like:

string GetParameterName(LambdaExpression expression) => expression.Parameters.Single().Name;
4
11/13/2017 7:34:13 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow