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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:41:41+00:00 2026-06-07T12:41:41+00:00

Similar: Convert a string to Linq.Expressions or use a string as Selector? A similar

  • 0

Similar: Convert a string to Linq.Expressions or use a string as Selector?

A similar one of that one: Passing a Linq expression as a string?

Another question with the same answer: How to create dynamic lambda based Linq expression from a string in C#?

Reason for asking something which has so many similar questions:

The accepted answer in those similar questions is unacceptable in that they all reference a library from 4 years ago (granted that it was written by code master Scott Gu) written for an old framework (.net 3.5) , and does not provide anything but a link as an answer.

There is a way to do this in code without including a whole library.

Here is some sample code for this situation:

    public static void getDynamic<T>(int startingId) where T : class
    {
        string classType = typeof(T).ToString();
        string classTypeId = classType + "Id";
        using (var repo = new Repository<T>())
        {
            Build<T>(
             repo.getList(),
             b => b.classTypeId //doesn't compile, this is the heart of the issue
               //How can a string be used in this fashion to access a property in b?
            )
        }
    }

    public void Build<T>(
        List<T> items, 
        Func<T, int> value) where T : class
    {
        var Values = new List<Item>();
        Values = items.Select(f => new Item()
        {
            Id = value(f)
        }).ToList();
    }

    public class Item
    {
     public int Id { get; set; }
    }

Note that this is not looking to turn an entire string into an expression such as

query = "x => x.id == somevalue";

But instead is trying to only use the string as the access

query = x => x.STRING;
  • 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-07T12:41:44+00:00Added an answer on June 7, 2026 at 12:41 pm

    Here’s an expression tree attempt. I still don’t know if this would work with Entity framework, but I figure it is worth a try.

    Func<T, int> MakeGetter<T>(string propertyName)
    {
        ParameterExpression input = Expression.Parameter(typeof(T));
    
        var expr = Expression.Property(input, typeof(T).GetProperty(propertyName));
    
        return Expression.Lambda<Func<T, int>>(expr, input).Compile();  
    }
    

    Call it like this:

    Build<T>(repo.getList(), MakeGetter<T>(classTypeId))
    

    If you can use an Expression<Func<T,int>> in place of a just a Func, then just remove the call to Compile (and change the signature of MakeGetter).


    Edit:
    In the comments, TravisJ asked how he could use it like this: w => "text" + w.classTypeId

    There’s several ways to do this, but for readability I would recommend introducing a local variable first, like this:

    var getId = MakeGetter<T>(classTypeId);
    
    return w => "text" + getId(w); 
    

    The main point is that the getter is just a function, and you can use it exactly like you normally would. Read Func<T,int> like this: int DoSomething(T instance)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Similar posts such as the following do not answer my question. Convert a string
This is a similar question to this one . I would like to convert
I've looked for a similar question, but the only one that was close didn't
Similar to this question I am using a custom VirtualPathProvider to retrieve views that
Similar to toString is there a way we can convert a string to BSON
The question and answer of converting a class to another list of class is
I would like to convert the below foreach statement to a LINQ query that
My question is similar to the one here: Select a specific column based on
There are some situation that I need to convert string to float or some
Is there an easy way to convert a string that contains this: Date: Wed,

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.