Search code examples
linqexpression-treeslinq-to-nhibernatedynamic-linq

Extract input parameter name from lambda expression


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?


Solution

  • 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;