Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How convert SQL Server Pivot functionality in C# uisng LINQ

How convert SQL Server Pivot functionality in C# uisng LINQ

Scheduled Pinned Locked Moved C#
csharpdatabasesql-serverlinqsysadmin
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mou_kol
    wrote on last edited by
    #1

    I am using SQL Server Pivot which works fine. Here is sample data below which transpose by SQL server pivot function. this is sample data which convert to pivot by SQL server.

    +------------+--------------+-----------+--------------+--------+-------------+--------------------+----------+
    | RowNumber | Section | LineItem | DisplayInCSM | Broker | BrokerName | ItemValue_NoFormat | Period |
    +------------+--------------+-----------+--------------+--------+-------------+--------------------+----------+
    | 1 | Operational | NG Sales | NGL | CR | Credit Suse | 200 | 2010 FYA |
    | 2 | Operational | NG Sales | NGL | GR | Max 1 | 300 | 2010 FYA |
    | 3 | Operational | NG Sales | NGL | PX | Morgan | 100 | 2010 FYA |
    | 4 | Operational | NG Sales | NGL | WB | Wells Fargo | 500 | 2010 FYA |
    +------------+--------------+-----------+--------------+--------+-------------+--------------------+----------+

    This is dynamic sql i used in sql server to represent data in pivot format. here it is.

    SET @SQL='SELECT *
    FROM
    (
    SELECT RowNumber,CAST(ISNULL(EarningID,0) AS INT) EarningID,
    Section,
    LineItem,
    DisplayInCSM,
    Type,
    Broker,
    BrokerName,
    ItemValue_NoFormat,
    TRIM(ISNULL(Period,'''')) Period,hierarchy,
    from #tmpData1 WHERE TYPE<>''SHEET''
    ) t
    PIVOT
    (
    MAX(ItemValue_NoFormat)
    FOR Broker IN ([5W], [8K], [CL], [DA], [EQ], [FA], [GS], [HM], [HQ], [JY], [KW], [ML], [MS], [MV], [SL], [UA],[WB])
    ) AS P
    order by hierarchy,PeriodOrder

    L 1 Reply Last reply
    0
    • M Mou_kol

      I am using SQL Server Pivot which works fine. Here is sample data below which transpose by SQL server pivot function. this is sample data which convert to pivot by SQL server.

      +------------+--------------+-----------+--------------+--------+-------------+--------------------+----------+
      | RowNumber | Section | LineItem | DisplayInCSM | Broker | BrokerName | ItemValue_NoFormat | Period |
      +------------+--------------+-----------+--------------+--------+-------------+--------------------+----------+
      | 1 | Operational | NG Sales | NGL | CR | Credit Suse | 200 | 2010 FYA |
      | 2 | Operational | NG Sales | NGL | GR | Max 1 | 300 | 2010 FYA |
      | 3 | Operational | NG Sales | NGL | PX | Morgan | 100 | 2010 FYA |
      | 4 | Operational | NG Sales | NGL | WB | Wells Fargo | 500 | 2010 FYA |
      +------------+--------------+-----------+--------------+--------+-------------+--------------------+----------+

      This is dynamic sql i used in sql server to represent data in pivot format. here it is.

      SET @SQL='SELECT *
      FROM
      (
      SELECT RowNumber,CAST(ISNULL(EarningID,0) AS INT) EarningID,
      Section,
      LineItem,
      DisplayInCSM,
      Type,
      Broker,
      BrokerName,
      ItemValue_NoFormat,
      TRIM(ISNULL(Period,'''')) Period,hierarchy,
      from #tmpData1 WHERE TYPE<>''SHEET''
      ) t
      PIVOT
      (
      MAX(ItemValue_NoFormat)
      FOR Broker IN ([5W], [8K], [CL], [DA], [EQ], [FA], [GS], [HM], [HQ], [JY], [KW], [ML], [MS], [MV], [SL], [UA],[WB])
      ) AS P
      order by hierarchy,PeriodOrder

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Mou_kol wrote:

      i saw these post Is it possible to Pivot data using LINQ?

      Prolly is, but why move that to the client in the first place? Why do you prefer to hold a dataset in memory, when it is more efficient on the server?

      Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

      M 1 Reply Last reply
      0
      • L Lost User

        Mou_kol wrote:

        i saw these post Is it possible to Pivot data using LINQ?

        Prolly is, but why move that to the client in the first place? Why do you prefer to hold a dataset in memory, when it is more efficient on the server?

        Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

        M Offline
        M Offline
        Mou_kol
        wrote on last edited by
        #3

        SQL server taking long time to return result set and we have network problem. also we do not have a DBA. using my knowledge i am not being able to suppress the issue. so decided to move the whole part in c#. so please guide me how to achieve it in c# alone. thanks

        L P 3 Replies Last reply
        0
        • M Mou_kol

          SQL server taking long time to return result set and we have network problem. also we do not have a DBA. using my knowledge i am not being able to suppress the issue. so decided to move the whole part in c#. so please guide me how to achieve it in c# alone. thanks

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Mou_kol wrote:

          SQL server taking long time to return result set and we have network problem. also we do not have a DBA

          You missing DBA is prolly reason why it takes that time.

          Mou_kol wrote:

          using my knowledge i am not being able to suppress the issue

          Welcome to my world.

          Mou_kol wrote:

          so decided to move the whole part in c#

          Without knowing if that would improve anything?

          Mou_kol wrote:

          so please guide me how to achieve it in c# alone. thanks

          No. Ethics. Not going to reccomend you wrong because you ask/want/need it. Get someone who knows databases. Profit that way.

          Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

          1 Reply Last reply
          0
          • M Mou_kol

            SQL server taking long time to return result set and we have network problem. also we do not have a DBA. using my knowledge i am not being able to suppress the issue. so decided to move the whole part in c#. so please guide me how to achieve it in c# alone. thanks

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Your answer to meeting a deficiency is to ignore it and pursue a less efficient alternative. (You can look at it as a question or a statement).

            It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

            L 1 Reply Last reply
            0
            • L Lost User

              Your answer to meeting a deficiency is to ignore it and pursue a less efficient alternative. (You can look at it as a question or a statement).

              It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Damn. You brutal :D

              Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

              L 1 Reply Last reply
              0
              • L Lost User

                Damn. You brutal :D

                Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Existentialism.

                It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

                1 Reply Last reply
                0
                • M Mou_kol

                  SQL server taking long time to return result set and we have network problem. also we do not have a DBA. using my knowledge i am not being able to suppress the issue. so decided to move the whole part in c#. so please guide me how to achieve it in c# alone. thanks

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #8

                  This could mean anything. When you say that SQL Server is taking a long time to return the result set, you have left a wide open area here. Does the pivot table take a long time to create on the server? Is it returning a massive amount of data? Has anyone run a profiling session on the pivot operation to see if you are doing sequential scans? Doing an analysis in SQL Server does not need a DBA - the developers should be able to pick this up. In reality, I doubt you could build a pivot that would work as quickly as the SQL Server implementation which is heavily optimised. In order to pivot, you would have to pull all of the data back from the server then write your own code to transform the data, applying aggregation operations yourself, so you will need to potentially drag back large amounts of data. My advice - profile the PIVOT operation.

                  Advanced TypeScript Programming Projects

                  OriginalGriffO 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    This could mean anything. When you say that SQL Server is taking a long time to return the result set, you have left a wide open area here. Does the pivot table take a long time to create on the server? Is it returning a massive amount of data? Has anyone run a profiling session on the pivot operation to see if you are doing sequential scans? Doing an analysis in SQL Server does not need a DBA - the developers should be able to pick this up. In reality, I doubt you could build a pivot that would work as quickly as the SQL Server implementation which is heavily optimised. In order to pivot, you would have to pull all of the data back from the server then write your own code to transform the data, applying aggregation operations yourself, so you will need to potentially drag back large amounts of data. My advice - profile the PIVOT operation.

                    Advanced TypeScript Programming Projects

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #9

                    :thumbsup:

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups