Code to Swap Rows and Columns (Pivot) the Results of a Linq Query

Posted on 2012-08-29
Medium Priority
Last Modified: 2012-09-23
Good evening, I am faced with having to do the following (and preferably not having to manually code something and loop through the results repeatedly).  We have a table with about 30 fields in it which we run a Linq query against to pull a set of results that might be a something like the following (subset of data below):

ID     DATE           BP           HEIGHT     WEIGHT     PULSE
1      1/1/12     120/70           6'0           210             80
2      1/8/12     120/70           6'0           212             76
3      1/15/12   120/70           6'0           214             60

and I need to turn it into this:

DATE             1/1/12               1/8/12            1/15/12
BP                 120/70              120/70             120/70
HEIGHT            6'0                     6'0                   6'0
WEIGHT          210                    212                 214  
PULSE              80                      76                     60

Now I know this is probably the simplest thing in the world, but it has been a long time since programming classes for me :)  so I am looking for some help.

There are actually about 30 fields in the source table and an average of about 20 results per query we will need to format in this manner.  It is basically all records tracking as you may have guessed some data about a persons health status over a period of time.

Thanks in advance!
Question by:bas2754
  • 3
LVL 18

Expert Comment

ID: 38351121
this is the answer very similar for your  needs  and data columns:
Using lambda or LINQ for pivot tables

Author Comment

ID: 38356290
That really isn't at all what I am trying to do.  I need to dynamically create each column based on the data that is in the date field.  I do not need to sum or count any information, just display it in the format shown above.

In all the examples so far that I have found, the fields have needed to be hard coded into the LINQ query - including the example in the link above.

I basically need to be able to take the column field names from the original table and create a row from each of those column field names with the proper value for that field inserted into the date column of the newly created table.

I am about to just give up and write a very messy set of loops for this, but was hoping for a more elegant LINQ solution.  

I am looking for a code snipped example that might be thought out more than just a link to here is something like what you want to do.  Please post code that would turn that first table into the second if it is doable.  

Thank You again in advance.

Accepted Solution

bas2754 earned 0 total points
ID: 38409640
Ended up coding this in a series for for next loops.

Author Closing Comment

ID: 38426007
Seems that either no one else had a better idea or that no one had a more specific example.  The link provided by the one responder was one I had seen already, but as stated I am looking for a way to not have to code any field names into the query.

Unfortunately Links are not answers and since no one else commented or offered solutions and I needed to get this done, I ended up doing it the old fashioned way.

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question