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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:05:00+00:00 2026-06-01T10:05:00+00:00

Consider this is a standard Linq from q in SomeQuery select new SomeObject {

  • 0

Consider this is a standard Linq

from q in SomeQuery select new SomeObject { SomeKey = q.SomeKey }

What if the Somekey is a text I got from other place, then the code like this is not going to work

string key = "SomeKey";
from q in SomeQuery select new SomeObject { key = q[key] }

I hope you got my idea. Please tell me a correct way to write Linq, when I know the property by a text 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-01T10:05:01+00:00Added an answer on June 1, 2026 at 10:05 am

    In the general case you are looking for something like

    var key = "SomeKey";
    from q in SomeQuery select new SomeObject { 
        SomeKey = q.GetType().GetProperty("key").GetValue(q, null)
    };
    

    This fetches the value of a property known at runtime with reflection, but I find it hard to recommend code like this. Let’s see why.

    1. First of all, it’s not possible to dynamically define the name of the property in the anonymous type we are selecting (here, I have hardcoded it as SomeKey). Anonymous types may not have a name, but they are types like any other and in a language like C# this means that their members have to be known at compile time. So it would be theoretically impossible to create the anonymous type at runtime¹. Even if it were possible to do it, you could not access the properties on the anonymous type without, again, using reflection.

    2. It does not make much sense to “store the name of the information” as a property name (which is static) when you already have it in a variable (which is dynamic). If you want to pass a reference around, pass the contents of key.

    3. If you want to associate string keys with values it makes sense to use a dynamic structure as your backing store. For example, if SomeQuery is IEnumerable<IDictionary<string, object>> then you could simply do SomeKey = q[key]. If not, then perhaps it should have been?

    4. If for some reason you need all this dynamism, then a reasonable first approach would be to look into using ExpandoObject.

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

Sidebar

Related Questions

Consider this code... using System.Threading; //... Timer someWork = new Timer( delegate(object state) {
consider this code block public void ManageInstalledComponentsUpdate() { IUpdateView view = new UpdaterForm(); BackgroundWorker
Consider this trigger: ALTER TRIGGER myTrigger ON someTable AFTER INSERT AS BEGIN DELETE FROM
I am using bcc32 command line compiler from Borland Embarcadero. Consider this program: int
Consider this following code (that retrieves the response from a HTTP request and prints
Consider this: Requisite: //The alphabet from a-z List<char> letterRange = Enumerable.Range('a', 'z' - 'a'
Follow up question from What is a "rooted reference"? consider this code, for a
Consider this problem: I have a program which should fetch (let's say) 100 records
Consider this case: dll = LoadDLL() dll->do() ... void do() { char *a =
Consider this Python program which uses PyGtk and Hippo Canvas to display a clickable

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.