Similar to question How can I get property name strings used in a Func of T.
Let’s say I had a lambda expression
stored like this in a variable called “getter”
Expression<Func<Customer, string>> productNameSelector =
customer => customer.Product.Name;
how can I extract the string “Product.Name” from that?
I now fixed it somewhat haxy with
var expression = productNameSelector.ToString();
var token = expression.Substring(expression.IndexOf('.') + 1);
But i’d like to find a more solid way 😉
The expression tree for your expression looks like this:
As you can see, there is no node that would represent
Product.Name. But you can use recursion and build the string yourself: