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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:02:28+00:00 2026-05-31T01:02:28+00:00

I want to make a generalized Data Table to Linq Collection I am a

  • 0

I want to make a generalized Data Table to Linq Collection
I am a beggginer so if it’s not possible please let me know

public void Something(DataTable dt)
{
    var data = from row in dt.AsEnumerable()
               select new { 
                            Order = row["Order"].ToString(), 
                            Something = row["Something"].ToString(),
                            Customer = row["Customer"].ToString(),
                            Address = row["Address"].ToString()
                          };
}

That is the code for one table
i want something like this:

    public static void convertDatatable(DataTable dt)
    {
        var results = from myRow in dt.AsEnumerable()
                      select new
                      {
                          foreach(DataColumn column in dt.Columns)
                              column.ColumnName // linq Variable name
                                  = myRow[column.ColumnName];// linq Variable Value
                      };
    }

I know it doesn’t work how i wrote it but is there another way ?

Note: the reason i am doing this is because i can’t convert Datatable directly to JSON it serializes it to XMl then sends it as a string containing that xml.

  • 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-31T01:02:29+00:00Added an answer on May 31, 2026 at 1:02 am

    If you want to stay with datatables then there is this, mentioned in another SO: What should I use to serialize a DataTable to JSON in ASP.NET 2.0?, which links to What should I use to serialize a DataTable to JSON in ASP.NET 2.0?.

    I highly recommend, however, that you consider moving away from DataTables and DataRows, replacing it instead with an ORM such as Entity Framework (EF Quick Start here) or Linq to Sql – there are others, but since you are a beginner these offer the easiest learning curve; not least because of the full designer support in Visual Studio.

    For the standard forms of JSON serialization offered by .Net (e.g. WCFs DataContractSerializer or the Asp.Net JSON serializer) then you need concrete types. The ORM solution will create all your table wrapper types at design-time, giving you a concrete type, potentially, for every table in your database.

    As for the idea you’ve specifically outlined above, it is exceptionally difficult to achieve – because the compiler, in the first example, dynamically generates a type whose members match the names and types of the expressions you use. If you open your compiled code in ILSpy and switch to IL instead of C# you’ll see what I mean.

    Therefore, to reproduce it dynamically you would need to dynamically emit a class, probably using ILGenerator, doing the same thing; and then dynamically emit the expression tree (using the Expression class’ static factory methods) to fill it out; and finally compile and execute it.

    I would only look at doing something like that if I literally couldn’t do it any other way – I’d be more likely to just write a routine to iterate through each column and write the JSON to a StringBuilder and return that! But if I could use an ORM, then I’d do that instead.

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

Sidebar

Related Questions

I want make datetimepicker in my project. Using jquery how it is possible? I
Let's say I want make some of my sources publicly available via my blog
I want to make a table in SqlServer that will add, on insert, a
I want make sql query which will insert values from one table to another
Want make a mind map like the following: Image link is not available anymore.
I want make a xslt transformation to xsl-fo but I´m not really sure that
i want make function like that function(data){ } i want this data as this
I want to make some graphical dialogs for my script but don't know how.
I make Iphone application and I want make my table view to look like
I am newer for php. I want make php page cache, query data from

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.