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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:41:58+00:00 2026-06-16T21:41:58+00:00

Trying to pivot dynamic data using LINQ or LAMBDA in C#/MVC4 and have pretty

  • 0

Trying to pivot dynamic data using LINQ or LAMBDA in C#/MVC4 and have pretty much come to the conclusion that its very difficult to do..

This is basically what I want to do:
Pivot

I have been able to get this to work with known column names using this example: http://geekswithblogs.net/malisancube/archive/2009/04/21/using-lambda-or-linq-for-pivot-tables.aspx

But I cant find any examples for doing this with dynamic columns.

By dynamic columns I mean that there could be a new row with a different Name and FieldType that has not been in the table before at any time that also needs to be turned into a column.. any pointers?.

  • 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-06-16T21:41:59+00:00Added an answer on June 16, 2026 at 9:41 pm

    I don’t know LINQ so I will give you a version that can be used in a SQL Server stored procedure. This type of data transformation is known as a PIVOT. Since you are using SQL Server 2008+, you can use the function.

    If you know the values that you want to transform, then you can hard-code the values:

    SELECT nodeid, rowid,[FirstName], [LastName], [Title]
    FROM
    (
        SELECT nodeid, rowid, name, value
        FROM yourTable
    ) x
    PIVOT
    (
       max(value)
       for name in ([FirstName], [LastName], [Title])
    )p
    

    See SQL Fiddle with Demo

    Then if you have an unknown number of values, you can implement dynamic SQL:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(name) 
                        from yourtable
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT nodeid, rowid,' + @cols + ' from 
                 (
                    SELECT nodeid, rowid, name, value
                    FROM yourTable
                ) x
                pivot 
                (
                    max(value)
                    for name in (' + @cols + ')
                ) p '
    
    execute(@query)
    

    See SQL Fiddle with Demo

    Both return the results:

    | NODEID | ROWID | FIRSTNAME | LASTNAME |  TITLE |
    --------------------------------------------------
    |      1 |     1 |    Alfred |   Beagle | (null) |
    |      1 |     2 |    Freddy |   (null) | (null) |
    |      1 |     3 |    (null) |     Grey |   Sir. |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying out ExtJS Pivot Grid. I'm using the examples pretty much as-is from
I have a pivot table with some data that im trying to reference with
I'm trying to pivot some data in SQL Server 2000, but the user that
I have been trying to move away from using DECODE to pivot rows in
I'm trying to use Linq to pivot a data table. Trawling through the responses
I have been trying to add data labels to my pivot charts programmatically through
I have a query where I'm trying pivot row values into column names and
I'm trying to create a query with dynamic columns, based on data from three
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Every example of using PIVOT in MSSQL shows people using this to aggregate data.

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.