Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8065483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:40:42+00:00 2026-06-05T11:40:42+00:00

Similar to question How can I get property name strings used in a Func

  • 0

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 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:40:43+00:00Added an answer on June 5, 2026 at 11:40 am

    The expression tree for your expression looks like this:

               .
              / \
             .   Name
            / \
    customer   Product
    

    As you can see, there is no node that would represent Product.Name. But you can use recursion and build the string yourself:

    public static string GetPropertyPath(LambdaExpression expression)
    {
        return GetPropertyPathInternal(expression.Body);
    }
    
    private static string GetPropertyPathInternal(Expression expression)
    {
        // the node represents parameter of the expression; we're ignoring it
        if (expression.NodeType == ExpressionType.Parameter)
            return null;
    
        // the node is a member access; use recursion to get the left part
        // and then append the right part to it
        if (expression.NodeType == ExpressionType.MemberAccess)
        {
            var memberExpression = (MemberExpression)expression;
    
            string left = GetPropertyPathInternal(memberExpression.Expression);
            string right = memberExpression.Member.Name;
    
            if (left == null)
                return right;
    
            return string.Format("{0}.{1}", left, right);
        }
    
        throw new InvalidOperationException(
            string.Format("Unknown expression type {0}.", expression.NodeType));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a similar question How can I assign the result of a subroutine
I ask a similar question here and Darin Dimitrov answer that we can't use
I can't resolve this issue, I found a similar question here but: setting the
This is very similar to another recent question: How can I return the current
My question is similar to this one , but I can simplify it some.
I know this question is similar to several previous ones, but I can't find
Maybe is a often repeated question here, but i can't find anything similar with
This may be a very dumb question but I can't seem to get it
In jQuery, I can get a CSS property value for a selector using the
I asked a similar question last week but did not get an answer that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.