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. LINQ
  4. sql to linq.

sql to linq.

Scheduled Pinned Locked Moved LINQ
helpcsharpdatabaselinqquestion
5 Posts 4 Posters 9 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.
  • J Offline
    J Offline
    JimBob SquarePants
    wrote on last edited by
    #1

    Hi guys, I'm having real trouble converting this SQL statement into linq. I've been converting some code samples but have got stuck with this last one. Any Ideas? Your help would be truly appreciated!

    "SELECT AdId,Count(*) As Impressions
    FROM AdStats
    WHERE Type=0
    GROUP BY AdId
    ORDER BY Impressions DESC"

    Impressions is one of two databound fields in my gridview, (AdId is the other). What I need is a single result that I can use as the datasource for my gridview. I can see what the code is doing, I just can't replicate it. Thanks in advance.

    JimBob SquarePants ******************************************************************* "He took everything personally, including our royalties!" David St.Hubbins, Spinal Tap about Ian Faith, their ex-manager *******************************************************************

    J B J 3 Replies Last reply
    0
    • J JimBob SquarePants

      Hi guys, I'm having real trouble converting this SQL statement into linq. I've been converting some code samples but have got stuck with this last one. Any Ideas? Your help would be truly appreciated!

      "SELECT AdId,Count(*) As Impressions
      FROM AdStats
      WHERE Type=0
      GROUP BY AdId
      ORDER BY Impressions DESC"

      Impressions is one of two databound fields in my gridview, (AdId is the other). What I need is a single result that I can use as the datasource for my gridview. I can see what the code is doing, I just can't replicate it. Thanks in advance.

      JimBob SquarePants ******************************************************************* "He took everything personally, including our royalties!" David St.Hubbins, Spinal Tap about Ian Faith, their ex-manager *******************************************************************

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      Jim, one thing that stumps me with this query is the selecting of Count(*) and ordering by that. Count(*) will count the number of results returned from the AdStats table, but ordering by Count(*)? I don't understand that. (Mind you, I'm not a SQL expert by any means, so pardon me if this is a dumb question.)

      P 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        Jim, one thing that stumps me with this query is the selecting of Count(*) and ordering by that. Count(*) will count the number of results returned from the AdStats table, but ordering by Count(*)? I don't understand that. (Mind you, I'm not a SQL expert by any means, so pardon me if this is a dumb question.)

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

        You'd normally order by a count if you want your results ordered by items like popularity, etc.

        Deja View - the feeling that you've seen this post before.

        My blog | My articles

        1 Reply Last reply
        0
        • J JimBob SquarePants

          Hi guys, I'm having real trouble converting this SQL statement into linq. I've been converting some code samples but have got stuck with this last one. Any Ideas? Your help would be truly appreciated!

          "SELECT AdId,Count(*) As Impressions
          FROM AdStats
          WHERE Type=0
          GROUP BY AdId
          ORDER BY Impressions DESC"

          Impressions is one of two databound fields in my gridview, (AdId is the other). What I need is a single result that I can use as the datasource for my gridview. I can see what the code is doing, I just can't replicate it. Thanks in advance.

          JimBob SquarePants ******************************************************************* "He took everything personally, including our royalties!" David St.Hubbins, Spinal Tap about Ian Faith, their ex-manager *******************************************************************

          B Offline
          B Offline
          Brady Kelly
          wrote on last edited by
          #4

          SQL doesn't recognise aliases in places, so e.g.

          "SELECT AdId,Count(*) As Impressions
          FROM AdStats
          WHERE Type=0
          GROUP BY AdId
          HAVING Impressions > 10
          ORDER BY Impressions DESC"

          Will not work with the alias in the HAVING clause.  Maybe LINQ doesn't like the alias in your ORDER BY Impressions DESC, in favour of ORDER BY Count(*) DESC

          1 Reply Last reply
          0
          • J JimBob SquarePants

            Hi guys, I'm having real trouble converting this SQL statement into linq. I've been converting some code samples but have got stuck with this last one. Any Ideas? Your help would be truly appreciated!

            "SELECT AdId,Count(*) As Impressions
            FROM AdStats
            WHERE Type=0
            GROUP BY AdId
            ORDER BY Impressions DESC"

            Impressions is one of two databound fields in my gridview, (AdId is the other). What I need is a single result that I can use as the datasource for my gridview. I can see what the code is doing, I just can't replicate it. Thanks in advance.

            JimBob SquarePants ******************************************************************* "He took everything personally, including our royalties!" David St.Hubbins, Spinal Tap about Ian Faith, their ex-manager *******************************************************************

            J Offline
            J Offline
            JimBob SquarePants
            wrote on last edited by
            #5

            Thanks for your replies guys. I had a wee read and messed around. I think I have it cracked but i need to test it yet. What do you reckon of this? Makes sense?

            Dim result = From a In dc.OFG_AdStats _
            Group By AdId = a.AdId _
            Into Impressions = Count(a.Type = 0) _
            Order By Impressions Descending _
            Select AdId, Impressions

            Thanks

            JimBob SquarePants ******************************************************************* "He took everything personally, including our royalties!" David St.Hubbins, Spinal Tap about Ian Faith, their ex-manager *******************************************************************

            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