Vb.net 将数据表中的一列与同一数据表中的另一列相加
我有一个Datatable,它具有来自数据库的以下结果Vb.net 将数据表中的一列与同一数据表中的另一列相加,vb.net,Vb.net,我有一个Datatable,它具有来自数据库的以下结果 ID Name Class FeeName Amount 9 gumman hgjgh 6Th Fine 0 9 gumman hgjgh 6Th Tution Fee 3000 9 gumman hgjgh 6Th Lebority Fee 500 10 AAdil Hussain 5Th Fin
ID Name Class FeeName Amount
9 gumman hgjgh 6Th Fine 0
9 gumman hgjgh 6Th Tution Fee 3000
9 gumman hgjgh 6Th Lebority Fee 500
10 AAdil Hussain 5Th Fine 0
10 AAdil Hussain 5Th Tution Fee 3000
10 AAdil Hussain 5Th Lebority Fee 500
我想根据每个ID汇总金额,所以我实现了以下代码
Dim TotalAmount As New DataColumn("TotalAmount", GetType(Double), "Sum(Amount)")
Dtable.Columns.Add(TotalAmount)
但结果如下
ID Name Class FeeName Amount TotalAmount
9 gumman hgjgh 6Th Fine 0 7000
9 gumman hgjgh 6Th Tution Fee 3000 7000
9 gumman hgjgh 6Th Lebority Fee 500 7000
10 AAdil Hussain 5Th Fine 0 7000
10 AAdil Hussain 5Th Tution Fee 3000 7000
10 AAdil Hussain 5Th Lebority Fee 500 7000
但我想把每个名字或身份证的金额加起来
我不知道LINQ恐怕
数据表的
不能求和(金额)超过(按ID划分)
以下是LINQ解决方案:
Dim idGroupAmounts =
From row In DTable
Group row By ID = row.Field(Of Int32)("ID") Into IDRowGroup = Group
Select New With {
.ID = ID,
.IDRows = IDRowGroup,
.IdSum = IDRowGroup.Sum(Function(row) row.Field(Of Double)("Amount"))
}
For Each idGrpAmount In idGroupAmounts
Dim id = idGrpAmount.ID
Dim totalAmount = idGrpAmount.IdSum
Dim rows = idGrpAmount.IDRows
Next
很抱歉,
数据表的
不能按ID划分的总和(金额)
以下是LINQ解决方案:
Dim idGroupAmounts =
From row In DTable
Group row By ID = row.Field(Of Int32)("ID") Into IDRowGroup = Group
Select New With {
.ID = ID,
.IDRows = IDRowGroup,
.IdSum = IDRowGroup.Sum(Function(row) row.Field(Of Double)("Amount"))
}
For Each idGrpAmount In idGroupAmounts
Dim id = idGrpAmount.ID
Dim totalAmount = idGrpAmount.IdSum
Dim rows = idGrpAmount.IDRows
Next
谢谢你的帮助。我在sql查询中使用了
SUM(Amount)OVER(partitionbyid)
,我希望从中获得结果,因此找到了所需的输出。谢谢帮助。我在sql查询中使用了SUM(Amount)OVER(partitionbyid)
,从中我想要得到结果,所以我找到了我想要的输出。