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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:36:53+00:00 2026-05-18T19:36:53+00:00

Say i have a DataTable object with data from the database, and I need

  • 0

Say i have a DataTable object with data from the database, and I need to reorganize it. I think (according to my research) LINQ would be an elegant (but is it fast?) way to do so. The question is how.

The data comes from the database in the following format

ORDER  SAMPLE  METHOD    

1234   1      ASH-10        
1234   1      ASH-11   
1234   1      ASH-12   
1234   2      ASH-10    
1234   2      ASh-12
1234   5      ASH-10
1235   1      BSH-10
1235   1      BSH-11
1235   1      BSH-12

I need it to be reshuffled as

ORDER  SAMPLE ASH-10 ASH-11 ASH-12 BSH-10 BSH-11 BSH-12 
1234    1      Y       Y      Y     N        N    N
1234    2      Y       N      Y     N        N    N
1234    5      Y       N      N     N        N    N
1235    1      N       N      N     N        Y    Y

As you can see the method columns will be dynamic. I could, of course, loop through the data using the old fashioned loop, but it seems like LINQ would be a better and faster way to go about it. Correct me if I am wrong.

  • 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-18T19:36:53+00:00Added an answer on May 18, 2026 at 7:36 pm

    The DataTable is great for retrieving data from the database into your application, but once you have the data you are usually better off converting it into custom objects.

    Your preliminary design might look like this:

     class AllOrders
     {
        public static List<Order> Orders = new List<Order>();
        public static List<Method> Methods = new List<Method>();
    
        public static void ProcessTable(DataTable dt)
        {
            foreach (DataRow dr in dt.Rows)
            {
                int sampleId = Convert.ToInt32(dr["SAMPLE"]);
                string methodName = Convert.ToString(dr["Method"]);
                AddOrder(sampleId, methodName);
            }
        }
    
        private static void AddOrder(int SampleId, string methodName)
        {
            Method m = Methods.FirstOrDefault(x => x.Name == methodName);
            if (m == null)
            {
                m = new Method(methodName);
                Methods.Add(m);
            }
            Order o = new Order(SampleId, m);
            Orders.Add(o);
        }
    }
    
    class Order
    {
        public Order(int sampleId, Method m)
        {
            this.Method = m;
            this.SampleId = sampleId;
        }
        public int SampleId; //a more mature design might have a Sample class instead.
        public Method Method;
    
    }
    
    class Method
    {
        public string Name;
        public Method(string name)
        {
            this.Name = name;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say have something like: SELECT energy_produced, energy_consumed, timestamp1 AS timestamp FROM ( SELECT
Say you have an application divided into 3-tiers: GUI, business logic, and data access.
Say you have 2 database servers, one database is the 'master' database where all
I have a business object let's say a Customer. I have a DAL method
Let's say we have a DataTable with rows say: ROW1 ROW2 ROW3 ROW4 ROW5
Let's say I have a class that has a MyCustomDatabaseAccess as a data member.
I have a data table in my database where I store various settings. Since
I have DataTable object and am binding it to a gridview in C#. I
I have a DataTable with 10 rows say one of the columns numbered 1
I have a DataTable, say pdt, in my code. I just want to select

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.