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 4085272
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:33:11+00:00 2026-05-20T18:33:11+00:00

I am wanting to create a MemberExpression knowing only the field name; eg: public

  • 0

I am wanting to create a MemberExpression knowing only the field name; eg:

public static Expression<Func<TModel, T>> GenerateMemberExpression<TModel, T>(string fieldName)
    {
        PropertyInfo fieldPropertyInfo;

        fieldPropertyInfo = typeof(TModel).GetProperty(fieldName);

        var entityParam = Expression.Parameter(typeof(TModel), "e"); // {e}
        var columnExpr = Expression.MakeMemberAccess(entityParam, fieldPropertyInfo); // {e.fieldName}
        var lambda = Expression.Lambda(columnExpr, entityParam) as Expression<Func<TModel, T>>; // {e => e.column}

        return lambda;
    }

The problem with the above is that the field type must be strongly typed. Passing “object” in as the field type doesn’t work. Is there any way to generate this? Even Dynamic LINQ doesn’t appear to work.

  • 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-05-20T18:33:12+00:00Added an answer on May 20, 2026 at 6:33 pm

    There are a number of issues with your code:

    1. The parameter to your method is called fieldName, but you are getting a property out with it.
    2. You are using the non-generic Expression.Lambda method to generate the expression, which may choose an inappropriate delegate-type if the type-argument T passed to the method is not the same as the property-type. In this case, the as cast from the expression to the method’s return-type will fail and evaluate to null. Solution: Use the generic Lambda method with the appropriate type-arguments. No casting required.
    3. If you solve the second issue, things will work fine when a safe reference-conversion is available from the property-type to T, but not when more complicated conversions such as boxing / lifting are required. Solution: Use the Expression.Convert method where necessary.

    Here’s an update to your sample that addresses these issues:

    public static Expression<Func<TModel, T>> GenerateMemberExpression<TModel, T>
       (string propertyName)
    {
        var propertyInfo = typeof(TModel).GetProperty(propertyName);
    
        var entityParam = Expression.Parameter(typeof(TModel), "e"); 
        Expression columnExpr = Expression.Property(entityParam, propertyInfo);
    
        if (propertyInfo.PropertyType != typeof(T))
            columnExpr = Expression.Convert(columnExpr, typeof(T));
    
        return Expression.Lambda<Func<TModel, T>>(columnExpr, entityParam);
    }
    

    This will make all of the following calls succeed:

    GenerateMemberExpression<FileInfo, string>("Name");
    GenerateMemberExpression<string, int>("Length");
    
    // Reference conversion
    GenerateMemberExpression<FileInfo, object>("Name");          
    
    //Boxing conversion
    GenerateMemberExpression<string, object>("Length");
    
    //Lifted conversion
    GenerateMemberExpression<string, int?>("Length");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In R, I'm wanting to create a graph with x axis label expression(varname) ,
I am wanting to create a regular expression for the following scenario: If a
I was wanting to create a 2 dimensional string array that had dimensions string[5][*]
Wanting to create a static menu (IOS 5) and attempting to create custom cells
I'm wanting to create a read only text area in my app which allows
I am currently working with expression engine, and we are wanting to create a
I am wanting to create a VB.Net app that checks a database and then
I've been wanting to create a HUD style loading bar like the SMS app
What I am wanting to do is create a PDF ideally from HTML code.
Wanting to create custom data annotation validation. Are there any useful guides / samples

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.