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. Web Development
  3. ASP.NET
  4. VB Linq - Group invoices by store, and include all the invoice items

VB Linq - Group invoices by store, and include all the invoice items

Scheduled Pinned Locked Moved ASP.NET
csharplinqcomhelp
2 Posts 1 Posters 13 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
    jkirkerx
    wrote on last edited by
    #1

    I searched around the internet, but wasn't able to find much, and I lack the knowledge of using the correct terms to search with. I'm trying to group a bunch of invoices together by store code, and I want to bring the invoice items over as well. The invoice items are just a List(Of AtdMiniInvoiceItems), which matches the .FITEMS in the group, so I really don't need to use the With to itemize what I want. I'm looking for the correct way to do this without writing a ton of code, and a point in the right direction. I can see that perhaps using With and doing this again would be the correct solution, but was looking to just combine it all as is.

    'Group the Store Invoices by Store to Average out the Freight/Shipping Margins
    Dim gFreightInvoicesGrouped As List(Of AtdFreightInvoiceReport) = gFreightInvoicesAll _
    .OrderBy(Function(ob) ob.FSTORECODE) _
    .Where(Function(a) a.FSTORECODE = String.Empty Or a.FSTORECODE.ToLower() = "unknown") _
    .GroupBy(Function(inv) inv.FSTORECODE).Select(Function(cl) New AtdFreightInvoiceReport() With {
    .FINVNO = cl.First().FINVNO,
    .FSTORECODE = cl.First().FSTORECODE,
    .FLOCATION = cl.First().FLOCATION,
    .FAMOUNT = cl.Sum(Function(x) x.FAMOUNT),
    .FSHIPCOST = cl.Sum(Function(x) x.FSHIPCOST),
    .FSHIPPRICE = cl.Sum(Function(x) x.FSHIPPRICE),
    .FMARGINPERCENT = cl.Sum(Function(x) x.FMARGINPERCENT),
    .FREDFLAG = False,
    .FITEMS = cl.GroupBy(Function(shipItems) shipItems.FITEMS).ToList()
    }).ToList()

    If it ain't broke don't fix it Discover my world at jkirkerx.com

    J 1 Reply Last reply
    0
    • J jkirkerx

      I searched around the internet, but wasn't able to find much, and I lack the knowledge of using the correct terms to search with. I'm trying to group a bunch of invoices together by store code, and I want to bring the invoice items over as well. The invoice items are just a List(Of AtdMiniInvoiceItems), which matches the .FITEMS in the group, so I really don't need to use the With to itemize what I want. I'm looking for the correct way to do this without writing a ton of code, and a point in the right direction. I can see that perhaps using With and doing this again would be the correct solution, but was looking to just combine it all as is.

      'Group the Store Invoices by Store to Average out the Freight/Shipping Margins
      Dim gFreightInvoicesGrouped As List(Of AtdFreightInvoiceReport) = gFreightInvoicesAll _
      .OrderBy(Function(ob) ob.FSTORECODE) _
      .Where(Function(a) a.FSTORECODE = String.Empty Or a.FSTORECODE.ToLower() = "unknown") _
      .GroupBy(Function(inv) inv.FSTORECODE).Select(Function(cl) New AtdFreightInvoiceReport() With {
      .FINVNO = cl.First().FINVNO,
      .FSTORECODE = cl.First().FSTORECODE,
      .FLOCATION = cl.First().FLOCATION,
      .FAMOUNT = cl.Sum(Function(x) x.FAMOUNT),
      .FSHIPCOST = cl.Sum(Function(x) x.FSHIPCOST),
      .FSHIPPRICE = cl.Sum(Function(x) x.FSHIPPRICE),
      .FMARGINPERCENT = cl.Sum(Function(x) x.FMARGINPERCENT),
      .FREDFLAG = False,
      .FITEMS = cl.GroupBy(Function(shipItems) shipItems.FITEMS).ToList()
      }).ToList()

      If it ain't broke don't fix it Discover my world at jkirkerx.com

      J Offline
      J Offline
      jkirkerx
      wrote on last edited by
      #2

      Interesting, I didn't know we can run functions inside a Linq GroupBy. Hmm ... This opens up some new possibilities for me. I'll attack the invoice items again, report almost done now

      'Group the Store Invoices by Store to Average out the Freight/Shipping Margins
      Dim gFreightInvoicesGrouped As List(Of AtdFreightInvoiceReport) = gFreightInvoicesAll _
      .OrderBy(Function(ob) ob.FSTORECODE) _
      .Where(Function(a) a.FSTORECODE <> String.Empty Or a.FSTORECODE.ToLower() <> "unknown") _
      .GroupBy(Function(inv) inv.FSTORECODE).Select(Function(cl) New AtdFreightInvoiceReport() With {
      .FINVNO = cl.First().FINVNO,
      .FSTORECODE = cl.First().FSTORECODE,
      .FLOCATION = cl.First().FLOCATION,
      .FAMOUNT = cl.Sum(Function(x) x.FAMOUNT),
      .FSHIPCOST = cl.Sum(Function(x) x.FSHIPCOST),
      .FSHIPPRICE = cl.Sum(Function(x) x.FSHIPPRICE),
      .FMARGINPERCENT = AmCommon.CalculateShippingPercentageFormattedToString(cl.Sum(Function(x) x.FSHIPCOST), cl.Sum(Function(x) x.FAMOUNT)),
      .FREDFLAG = False
      }).ToList()

      If it ain't broke don't fix it Discover my world at jkirkerx.com

      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