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

  • Home
  • SEARCH
  • 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 715495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:11:37+00:00 2026-05-14T05:11:37+00:00

If you have a simple Linq query like: var result = from record in

  • 0

If you have a simple Linq query like:

var result = from record in db.Customer
             select new { Text = record.Name,
                          Value = record.ID.ToString() };

which is returning an object that can be mapped to a Drop Down List, is it possible to dynamically specify which fields map to Text and Value?

Of course, you could do a big case (switch) statement, then code each Linq query separately but this isn’t very elegant. What would be nice would be something like:

(pseudo code)

var myTextField = db.Customer["Name"];  // Could be an enumeration??
var myValueField = db.Customer["ID"];   // Idea: choose the field outside the query

var result = from record in db.Customer
             select new { Text = myTextField,
                          Value = myValueField };
  • 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-14T05:11:37+00:00Added an answer on May 14, 2026 at 5:11 am

    Right way to do this is with closures.

    Func<Customer, string> myTextField = (Customer c) => c["Name"];
    Func<Customer, int> myValueField = (Customer c) => c["ID"];
    
    var result = from record in db.Customer
                 select new { Text = myTextField(record),
                              Value = myValueField(record) };
    

    The one limitation is that your definition of myTextField always needs to return a string.

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

Sidebar

Related Questions

I have a LINQ query: var result = from mt in MessageTypes join mtfmt
I have a very simple linq query that gets data from two datatables (orderHeader
I have a very simple table and I can happily query it using LINQ
I have a simple Nhibernate Linq query that is returning more results than expected:
I have a simple SQL query: Select ID, COUNT(ID) as Selections, OptionName, SUM(Units) as
help me please with this simple E-sql query: var qStr = SELECT SqlServer.Month(o.DatePaid) as
I have a simple LINQ query that is going against a collection. Dim oList
I am learning LINQ and have a very simple question that I think will
I have simple regex \.*\ for me its says select everything between and ,
A simple linq to SQL query Might return a product object. Obiously I could

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.