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. Grouping question

Grouping question

Scheduled Pinned Locked Moved LINQ
csharpquestiondatabaselinq
5 Posts 3 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.
  • K Offline
    K Offline
    kanchoette
    wrote on last edited by
    #1

    I am trying to group my datatable data on column SourceID. But currently it just returns all rows. Here's my LINQ query:

    Dim transactionDetailsQuery = (From t In dc.SupplierTransactions _
    Join p In dc.Portfolios On t.Portfolio Equals p _
    Join b In dc.BankAccountPortfolios On p Equals b.Portfolio _
    Group By SourceID = t.SourceID, BankAccountCode = b.BankAccountCode, TranDate = t.TranDate, _
    Period = t.Period, Year = t.Year, TranType = t.TranType, Reference = t.Reference, _
    ID = t.ID, PortfolioCode = t.PortfolioCode, Description = t.Description, SupplierCode = t.SupplierCode Into g = Group, _
    NetAmount = Sum(t.NetAmount), TaxAmount = Sum(t.TaxAmount), PaidAmount = Sum(t.PaidAmount) _
    Select Transaction = ID, SourceID, BankAccountCode, TranDate, Period, Year, TranType, Reference, PortfolioCode, Description, NetAmount, TaxAmount, PaidAmount, SupplierCode)

    What should the correct syntax be please?

    G J 2 Replies Last reply
    0
    • K kanchoette

      I am trying to group my datatable data on column SourceID. But currently it just returns all rows. Here's my LINQ query:

      Dim transactionDetailsQuery = (From t In dc.SupplierTransactions _
      Join p In dc.Portfolios On t.Portfolio Equals p _
      Join b In dc.BankAccountPortfolios On p Equals b.Portfolio _
      Group By SourceID = t.SourceID, BankAccountCode = b.BankAccountCode, TranDate = t.TranDate, _
      Period = t.Period, Year = t.Year, TranType = t.TranType, Reference = t.Reference, _
      ID = t.ID, PortfolioCode = t.PortfolioCode, Description = t.Description, SupplierCode = t.SupplierCode Into g = Group, _
      NetAmount = Sum(t.NetAmount), TaxAmount = Sum(t.TaxAmount), PaidAmount = Sum(t.PaidAmount) _
      Select Transaction = ID, SourceID, BankAccountCode, TranDate, Period, Year, TranType, Reference, PortfolioCode, Description, NetAmount, TaxAmount, PaidAmount, SupplierCode)

      What should the correct syntax be please?

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

      Well, if you want to filter and not "return all rows" I would suggest the Where clause. If you want more than one row per group, I would suggest grouping by fewer fields (you are currently grouping by 11 of which 1 looks like a primary key id and another looks like a free text field that will rarely be the same from one record to the next).

      K 1 Reply Last reply
      0
      • G Gideon Engelberth

        Well, if you want to filter and not "return all rows" I would suggest the Where clause. If you want more than one row per group, I would suggest grouping by fewer fields (you are currently grouping by 11 of which 1 looks like a primary key id and another looks like a free text field that will rarely be the same from one record to the next).

        K Offline
        K Offline
        kanchoette
        wrote on last edited by
        #3

        My problem has been that if I only group on SourceId, I am then unable (as yet) to reference items in the Select portion of the query. If I have Select Transaction = t.ID, t.SourceID .. etc The t. reference throws a scope/undefined error. How can I reference ID and SourceID from within the select portion please?

        G 1 Reply Last reply
        0
        • K kanchoette

          My problem has been that if I only group on SourceId, I am then unable (as yet) to reference items in the Select portion of the query. If I have Select Transaction = t.ID, t.SourceID .. etc The t. reference throws a scope/undefined error. How can I reference ID and SourceID from within the select portion please?

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

          As I explained when you asked the first part of this question, doing a Group By will get you a group object. If you just want to get all the rows with all records that have the same SourceID together, do a sort with Order By. Then you will be able to reference the original objects in the select part of the query. Grouping is most useful when you are trying to get some sort of aggregate result such as the sum of all order amounts from a particular customer.

          1 Reply Last reply
          0
          • K kanchoette

            I am trying to group my datatable data on column SourceID. But currently it just returns all rows. Here's my LINQ query:

            Dim transactionDetailsQuery = (From t In dc.SupplierTransactions _
            Join p In dc.Portfolios On t.Portfolio Equals p _
            Join b In dc.BankAccountPortfolios On p Equals b.Portfolio _
            Group By SourceID = t.SourceID, BankAccountCode = b.BankAccountCode, TranDate = t.TranDate, _
            Period = t.Period, Year = t.Year, TranType = t.TranType, Reference = t.Reference, _
            ID = t.ID, PortfolioCode = t.PortfolioCode, Description = t.Description, SupplierCode = t.SupplierCode Into g = Group, _
            NetAmount = Sum(t.NetAmount), TaxAmount = Sum(t.TaxAmount), PaidAmount = Sum(t.PaidAmount) _
            Select Transaction = ID, SourceID, BankAccountCode, TranDate, Period, Year, TranType, Reference, PortfolioCode, Description, NetAmount, TaxAmount, PaidAmount, SupplierCode)

            What should the correct syntax be please?

            J Offline
            J Offline
            Jon Rista
            wrote on last edited by
            #5

            Your group is grouping by a LOT of fields, so I am guessing that the broad set of data for grouping is making each row unique. In LINQ to SQL, like SQL itself, when you group, to access non-grouped data you need to aggregate it. Also like SQL, if you group on a primary key (which I assume SourceID is), then each row is guaranteed to be unique, and there isn't any point in grouping. Assuming you have a simple set like this: TABLE PersonAges ID, FirstName, LastName, Age ---------------------------- 1, Jon, Doe, 24 2, Jane, Doe, 22 3, Roger, Doe, 48 4, Judy, Doe, 51 5, Jack, Smith, 47 6, Audrey, Smith, 44 And you want to find the average age of members of each last name:

            var agesByLastName = from pa in db.PersonAges
            group pa by pa.LastName into g
            select new { LastName = g.Key, AverageAge = g.Average(pa => pa.Age) };

            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