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. Group by without a Group

Group by without a Group

Scheduled Pinned Locked Moved LINQ
databasecsharplinqquestion
6 Posts 3 Posters 5 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.
  • H Offline
    H Offline
    Howard Richards
    wrote on last edited by
    #1

    In SQL you can write:

    SELECT SUM(a) as T1, SUM(b) AS T2 FROM Table

    But is it possible to write a From x in .. Group query *without* having a group by clause and group key?? What would be the LINQ equivalent to this? I've not seen anything like this in the MS examples.

    'Howard

    J G 2 Replies Last reply
    0
    • H Howard Richards

      In SQL you can write:

      SELECT SUM(a) as T1, SUM(b) AS T2 FROM Table

      But is it possible to write a From x in .. Group query *without* having a group by clause and group key?? What would be the LINQ equivalent to this? I've not seen anything like this in the MS examples.

      'Howard

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

      Does this MSDN forum answer[^] help?

      Life, family, faith: Give me a visit. From my latest post: "The themes and truths of the Jewish holidays follow God's complete plan for this world. They are the root from which Christianity sprang and the historical reasons the church had for leaving them behind were unsound." Judah Himango

      H 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        Does this MSDN forum answer[^] help?

        Life, family, faith: Give me a visit. From my latest post: "The themes and truths of the Jewish holidays follow God's complete plan for this world. They are the root from which Christianity sprang and the historical reasons the church had for leaving them behind were unsound." Judah Himango

        H Offline
        H Offline
        Howard Richards
        wrote on last edited by
        #3

        Thanks but no, not quite! I know I can do .Sum() on a single value, e.g.

        var T1 = db.Table.Select(o => o.a).Sum();
        var T2 = db.Table.Select(o =gt; o.b).Sum();

        But obviously this is two queries to get two values, whereas my SQL is one query and therefore more efficient:

        SELECT SUM(a) as T1, SUM(b) AS T2 FROM Table

        At present this is as close as I can get, by using group by with a value that will create a single result row (this is VB, I could not figure out the C# syntax at all...

        Dim query = From t in Table _
        Group By dummy = false _
        Into T1 = Sum(t.a), T2 = Sum(t.b) _
        select T1, T2

        This does the same as the SQL query - but the "dummy = false" is a kludge I added to make Group By work.. hence the question!

        'Howard

        1 Reply Last reply
        0
        • H Howard Richards

          In SQL you can write:

          SELECT SUM(a) as T1, SUM(b) AS T2 FROM Table

          But is it possible to write a From x in .. Group query *without* having a group by clause and group key?? What would be the LINQ equivalent to this? I've not seen anything like this in the MS examples.

          'Howard

          G Offline
          G Offline
          Gideon Engelberth
          wrote on last edited by
          #4

          If you start a query with Aggregate instead, you can do something like that. The query would be: Aggregate t In Table Into T1 = Sum(t.a), T2 = Sum(t.b)

          H 2 Replies Last reply
          0
          • G Gideon Engelberth

            If you start a query with Aggregate instead, you can do something like that. The query would be: Aggregate t In Table Into T1 = Sum(t.a), T2 = Sum(t.b)

            H Offline
            H Offline
            Howard Richards
            wrote on last edited by
            #5

            Aha - so that's what the Aggregate keyword was for! Many thanks

            'Howard

            1 Reply Last reply
            0
            • G Gideon Engelberth

              If you start a query with Aggregate instead, you can do something like that. The query would be: Aggregate t In Table Into T1 = Sum(t.a), T2 = Sum(t.b)

              H Offline
              H Offline
              Howard Richards
              wrote on last edited by
              #6

              Guess what.. the SQL generated by LINQ to SQL runs two queries, not one!! Maybe MS needs to work on the query engine some..

              'Howard

              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