locked
Running Total Problem RRS feed

  • Question

  •  
    The following code works OK. It pull the data that I need out of the file Test.xml file. Inside the XML file, there is an element named <TotalCost>.
     How can I do a running total for this element using LINQ in VB after this code runs?


    Dim
    InvItems = XDocument.Load("C:\Test.xml")

    Dim TestID = <Inventory>

    <%= From itms In InvItems...<Table> _

    Order By CDec(itms.<TotalCost>.Value) Descending _

    Select <Item>

    <%= itms %>

    </Item> %>

    </Inventory>

         
       
     
     
         
    Post Options
    This option will split a single thread into a new thread at the specified post. Please include a subject for the new thread, a reason for the split and choose the forum where the new thread should be created. Note: any posts split will lose Proposed Answer or Answered state.
    This option will merge the currently selected thread into a target thread. Note: Answers and Proposed Answers will be removed after moving and this action cannot be undone. Please enter the thread ID to merge to. The thread ID is the portion of the URL after /thread/ which looks something like this: 8138b571-18b6-475a-85d0-63423e506875.
    This option allows the user to change the thread type. Note: changing the thread type will remove any Answers or Proposed Answers from question threads.
    Use the icon to add code.
    Has Code

    Subscribe to Alerts for this thread.
    Alert Me

    • Subject must be 1 to 255 characters long.
    • A reason is required between 4 and 255 characters.
    • Body must be 4 to 60000 characters long.
    • Please select a forum for your thread.
    • The tag contains illegal characters.
    • Sticky Date must be greater than current date.
    • Sticky Date format is invalid.
    • Invalid Thread ID
    • Required Field: Display Name must be between 2 and 32 characters
    • Invalid User: The user entered cannot be found or is not a Microsoft Employee
    Related Messages
    Monday, February 2, 2009 11:17 PM

Answers

  • Hi,

    I do not know if it still need an example here:

    Imports System.Linq
    
    Module Module1
    
        Sub Main()
            Dim InvItems = XDocument.Load("Test.xml")
    
            REM Solution 1
            Console.WriteLine("Solution 1")
            Dim TotalCost = From product In InvItems.Descendants("Table") _
                            Select (product.Element("SOH").Value * product.Element("Cost").Value)
    
            For Each item In TotalCost
                Console.WriteLine("Total cost {0}", item)
            Next
    
            REM Solution 2
            Console.WriteLine("Solution 2")
            Dim TotalCost2 = From product In InvItems.Descendants("Table") _
                             Select product
    
            Dim TEXT As String
            For Each item In TotalCost2
                TEXT = String.Format("Part: {1} {0} Total Cost: {2}", vbTab, item.Element("Part").Value, (item.Element("SOH").Value * item.Element("Cost").Value))
                Console.WriteLine(TEXT)
            Next
    
            REM Solution 3
            Console.WriteLine("Solution 3")
            Dim Part As String
            Dim SOH As Double
            Dim Cost As Double
            Dim Result As Double
            For Each item In TotalCost2
                Part = item.Element("Part").Value
                SOH = item.Element("SOH").Value
                Cost = item.Element("Cost").Value
                Result = SOH * Cost
                TEXT = String.Format("StockOnHand: {1}{0}CostPerUnit: {2}{0}Total Cost: {3}", vbTab, SOH, Cost, Result)
                Console.WriteLine("Part: {0}", Part)
                Console.WriteLine(TEXT)
            Next
            Console.ReadLine()
        End Sub
    
    End Module

    • Proposed as answer by SOWMAX Saturday, March 9, 2013 11:25 PM
    • Marked as answer by Just Karl Sunday, October 20, 2013 2:21 AM
    Saturday, March 9, 2013 11:23 PM

All replies

  • Can you post a sample of your XML document?
    MVP XML
    Tuesday, February 3, 2009 6:43 PM
  • Sure,

    The TotalCost element is just the StockOnHand (SOH) * the CostPerUnit(Cost)
    Here:

    <NewDataSet>
    - <Table>
      <BIN>D07C</BIN>
      <Bin2 />
      <Buyer>M</Buyer>
      <Part>001-PP3501000WA</Part>
      <Description>G-WA,TUBE,SP01-001</Description>
      <SOH>5</SOH>
      <Cost>13.7683</Cost>
      <Fin>P</Fin>
      <TotalCost>68.8415</TotalCost>
      </Table>
    - <Table>
      <BIN>8011</BIN>
      <Bin2 />
      <Buyer>M</Buyer>
      <Part>002-7050001</Part>
      <Description>G-COVER PLATE, ANTI-VIBRATION MOUNT</Description>
      <SOH>60</SOH>
      <Cost>1.8167</Cost>
      <Fin>P</Fin>
      <TotalCost>109.0020</TotalCost>
      </Table>
    - <Table>
    Tuesday, February 3, 2009 7:27 PM
  • Hi,

    I do not know if it still need an example here:

    Imports System.Linq
    
    Module Module1
    
        Sub Main()
            Dim InvItems = XDocument.Load("Test.xml")
    
            REM Solution 1
            Console.WriteLine("Solution 1")
            Dim TotalCost = From product In InvItems.Descendants("Table") _
                            Select (product.Element("SOH").Value * product.Element("Cost").Value)
    
            For Each item In TotalCost
                Console.WriteLine("Total cost {0}", item)
            Next
    
            REM Solution 2
            Console.WriteLine("Solution 2")
            Dim TotalCost2 = From product In InvItems.Descendants("Table") _
                             Select product
    
            Dim TEXT As String
            For Each item In TotalCost2
                TEXT = String.Format("Part: {1} {0} Total Cost: {2}", vbTab, item.Element("Part").Value, (item.Element("SOH").Value * item.Element("Cost").Value))
                Console.WriteLine(TEXT)
            Next
    
            REM Solution 3
            Console.WriteLine("Solution 3")
            Dim Part As String
            Dim SOH As Double
            Dim Cost As Double
            Dim Result As Double
            For Each item In TotalCost2
                Part = item.Element("Part").Value
                SOH = item.Element("SOH").Value
                Cost = item.Element("Cost").Value
                Result = SOH * Cost
                TEXT = String.Format("StockOnHand: {1}{0}CostPerUnit: {2}{0}Total Cost: {3}", vbTab, SOH, Cost, Result)
                Console.WriteLine("Part: {0}", Part)
                Console.WriteLine(TEXT)
            Next
            Console.ReadLine()
        End Sub
    
    End Module

    • Proposed as answer by SOWMAX Saturday, March 9, 2013 11:25 PM
    • Marked as answer by Just Karl Sunday, October 20, 2013 2:21 AM
    Saturday, March 9, 2013 11:23 PM