none
sum column from dataset RRS feed

  • Question

    • hi
    • I have a customer balances table 
      I fill in the table with all the data from ds = dsadd
    •  I want to sum direct column from the dataset where the customer has a special code
    • for example code = aa123  . 
    • I fill out the entire table for later use where I get the largest number and other processes
      Therefore, I do not place a query when the table is filled, provided that the columns are filled with a specific customer. The  following code sum each column.
    Dim result As Decimal = IIf(IsDBNull(DSADD.Tables("CUSTOMER_TB").Compute("SUM(CUSTOMER_CREDIT)", "")), "0", DSADD.Tables("CUSTOMER_TB").Compute("SUM(CUSTOMER_CREDIT)", ""))



    Sunday, December 23, 2018 4:53 PM

Answers

  • Here I load data into a DataSet then target a specific table. Operations is my own class to obtain data from SQL-Server, after data is loaded into a DataSet the data provider does not matter as we are now disconnected from the database.

    Private Sub cmdSumExample_Click(sender As Object, e As EventArgs) Handles cmdSumExample.Click
        Dim ops As New Operations
        ' load all data from database table
        Dim ds = ops.OrderDetails()
    
        ' here a valid id is provided, if not a valid id the result will be 0
        ' for a proper solution there should be zero chance of an invalid id
        Dim orderIdentifier As Integer = 10290
        Dim results = ds.Tables("Details").
                AsEnumerable().
                Where(Function(row) row.Field(Of Integer)("OrderId") = orderIdentifier).
                Select(Function(row) row.Field(Of Decimal)("UnitPrice") * row.Field(Of Int16)("Quantity")).Sum()
    
        Console.WriteLine($"Unformatted {results} Formatted {results.ToString("c2")}")
    
    End Sub

    Results

    Unformatted 0 Formatted $0.00

    I can always check if the identifier exists prior to running the code if by chance a user entered the identifier rather than the developer providing only valid values.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ahmeddc Sunday, December 23, 2018 9:23 PM
    Sunday, December 23, 2018 7:57 PM
    Moderator

All replies

  • Hi

    ..... and your question is ??????????


    Regards Les, Livingston, Scotland

    Sunday, December 23, 2018 5:03 PM
  • Hi

    ..... and your question is ??????????


    Regards Les, Livingston, Scotland

    I do not think you have read my question 
     
    •  I want to sum direct column from the dataset where the customer has a special code
    • for example code = aa123  


    • Edited by ahmeddc Sunday, December 23, 2018 5:14 PM
    Sunday, December 23, 2018 5:08 PM
  • Here I load data into a DataSet then target a specific table. Operations is my own class to obtain data from SQL-Server, after data is loaded into a DataSet the data provider does not matter as we are now disconnected from the database.

    Private Sub cmdSumExample_Click(sender As Object, e As EventArgs) Handles cmdSumExample.Click
        Dim ops As New Operations
        ' load all data from database table
        Dim ds = ops.OrderDetails()
    
        ' here a valid id is provided, if not a valid id the result will be 0
        ' for a proper solution there should be zero chance of an invalid id
        Dim orderIdentifier As Integer = 10290
        Dim results = ds.Tables("Details").
                AsEnumerable().
                Where(Function(row) row.Field(Of Integer)("OrderId") = orderIdentifier).
                Select(Function(row) row.Field(Of Decimal)("UnitPrice") * row.Field(Of Int16)("Quantity")).Sum()
    
        Console.WriteLine($"Unformatted {results} Formatted {results.ToString("c2")}")
    
    End Sub

    Results

    Unformatted 0 Formatted $0.00

    I can always check if the identifier exists prior to running the code if by chance a user entered the identifier rather than the developer providing only valid values.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ahmeddc Sunday, December 23, 2018 9:23 PM
    Sunday, December 23, 2018 7:57 PM
    Moderator