Question How to Compute sum("columnname1") where columnname2="" ?

ishta

New member
Joined
Jul 10, 2012
Messages
1
Programming Experience
1-3
Hi,
I have a datatable having 2 columns

CustomerName|| Total Price
--------------------------
A --> 10
B --> 20
----> 30
B --> 10
----> 30
C --> 10

There are 4 customers A,B,C and " " i.e blank.

I want to use compute function
datatable.compute(sum("total price"),"Customer Name=" " ") ---------> if Customer Name is blank .
also if possible i want to replace "" to blank i.e if customername.contains("") then customername.replace("","Blank")

the datatable after compute should look like below
Customer Name Total Price
A 10
B 30
Blank 60
C 10
 
Where do you get that datatable exactly? If you are running a query on a database, wouldn't it be simpler to modify the query?

If not, what you want is feasible through Linq:

Private Function GetSums(ByVal SourceTable As DataTable) As DataTable
    Dim SourceTable_Linq = SourceTable.AsEnumerable()
    Dim objSumResults = From r In SourceTable_Linq _
                        Group r By Key = r.Field(Of String)("CustomerName") Into Group _
                        Select objSumResults = Key, CustomerSum = Group.Sum(Function(r) r("TotalPrice"))
                            
    GetSums = objSumResults.CopyToDataTable()
    For Each row As DataRow In GetSums
        If row.Item("CustomerName") = "" Then row.Item("CustomerName") = "Blank"
    Next
End Function
 
Back
Top