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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:00:32+00:00 2026-05-23T03:00:32+00:00

This is a let’s see if it can be done exercise more than anything

  • 0

This is a “let’s see if it can be done” exercise more than anything else.

Suppose I have a DataTable with several columns. Using linq, is it possible to select the rows of the table such that the result will be a sequence of an anonymous type where each property of the anonymous type is named according to the column name of the DataTable and the type of each property is set appropriately.

So if my DataTable has three columns like so:

Name = "Column1",  DataType = string
Name = "Column2",  DataType = integer
Name = "Column3",  DataType = bool

Then I want to select all the rows of the DataTable such that the anonymous type has three properties:

DataSet ds = functionThatGetsADataSet();
var seq = ds.Tables[0].AsEnumerable().Select( r => **** MAGIC HERE ****)

foreach (var s in seq)
{
    Console.WriteLine(s.Column1);
    Console.WriteLine(s.Column2);
    Console.WriteLine(s.Column3);
}

I know that I can do this:

DataSet ds = functionThatGetsADataSet();
var seq = ds.Tables[0].AsEnumerable().Select( r => 
   new 
   {
       Column1 = r.Field<string>("Column1"),
       Column2 = r.Field<int>("Column2"),
       Column3 = r.Field<bool>("Column3"),
   }
)

foreach (var s in seq)
{
    Console.WriteLine(s.Column1);
    Console.WriteLine(s.Column2);
    Console.WriteLine(s.Column3);
}

but this requires hard coding the property names in the new clause, whereas I would like the property names to come from the Columns collection of the DataTable.

I hope that I have explained this clearly enough. After playing around with this for a little bit, I am beginning to think that anything that I come up with will be a big mess as far as readability and maintainability goes, but I thought I would ask.

  • 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-23T03:00:32+00:00Added an answer on May 23, 2026 at 3:00 am

    I’m afraid that there is no such magic (at least not with anonymous types). Anonymous types are implicit types generated by the compiler as immutable classes with their properties named & typed by the explicit assignment operation like in your second example (at compile time).

    What you’re wanting to do would require a dynamic type constructed at run time.

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

Sidebar

Related Questions

/// I can't do this let max = float n |> sqrt |> int64
I have an XML file, which I open in F# like this: let Bookmarks(xmlFile:string)
I have a bitmap image context and want to let this appear blurry. So
What is good design in this simple case: Let's say I have a base
I cannot find do...while... I have to code like this: let bubbleSort a= let
Could someone propose better and/or more elegant implementation of this: let each xs =
is it possible to do something like this: Let's say I have a lot
I can do this: let foo = bar |> baz but I get a
I'm looking for something like this: Let's say we have a string.. $chk=pie; And
Does php have the ability to do this: Let's say you have an observatory.

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.