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

The Archive Base Latest Questions

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

I have this object : public class MyObject { public int Id { get;

  • 0

I have this object :

public class MyObject
{
    public int Id { get; set; }
    public string FieldA { get; set; }
    public string FieldB { get; set; }
    public string FieldC { get; set; }
    public string FieldD { get; set; }
}

I have an IList<> of this object :

IList<MyObject> MyListObject = new List<MyObject>();

I do a Linq query on it :

var result = (from p in MyListObject where p.FieldC == "Test" select p.FieldA);

In this case, I return “p.FieldA” but sometimes I need to return “p.FieldB”. I’d like put the name of the field (FieldA or FieldB) in a variable like this

var myvar = "FieldB"
var result = (from p in MyListObject where p.FieldC == "Test" select p.????);

How use myvar content (FieldB) as field name in the Linq query ?

Thanks,

  • 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-20T23:11:35+00:00Added an answer on May 20, 2026 at 11:11 pm

    If your method itself knows it has to choose between FieldA and FieldB you can use:

    var whatField = "FieldA";
    var result = (from p in MyListObject 
         where p.FieldA == "Test" 
         select (whatField == FieldA ? p.FieldA : p.FieldB));
    

    If you have more as 2 options, I would choose to pass a lambda;

    Func<MyObject, object> fieldGetter;
    
    // Option A, switch
    switch (whatField) {
       case "FieldA": fieldGetter = o => o.FieldA; break;
       case "FieldB": fieldGetter = o => o.FieldB; break;
       // More options
    }
    
    // Option B using reflection:
    var t = typeof(MyObject);
    var prop = t.GetProperty(whatField);
    fieldGetter = o => prop.GetValue(o, null);
    
    // Linq then looks like
    var result = (from p in MyListObject 
         where p.FieldA == "Test" 
         select fieldGetter(p));
    

    The advantage of using the lambda is, you can split your logic, what field and how to query. And you can even make it work for different types:

    IEnumerable<T> Query<T>(IQueryable<MyObject> MyListObject, Func<MyObject, T> fieldGetter) {
      return result = (from p in MyListObject 
           where p.FieldA == "Test" 
           select fieldGetter(p));
    }
    
    // call using:
    
    var result = Query(MyListObject, o => o.FieldA);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class InvestorMailing { public string To { get; set; } public IEnumerable<string> Attachments
I have the following attempt at an immutable object: class MyObject { private static
I have this object graph, I want to map: abstract Account (username, password, ...)
In my qt app I have this object, filled before setting up my QTreeWidget's
Suppose I have this code: var myArray = new Object(); myArray["firstname"] = "Bob"; myArray["lastname"]
Suppose I have a design like this: Object GUI has two objects: object aManager
I have this ListBox which is bound to an ObservableCollection. Each object in the
I have a question about using new[] . Imagine this: Object.SomeProperty = new[] {string1,
So, I have this situation where I need to see if an object is
The situation is this: You have a Hibernate context with an object graph 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.