1

我有一个具有以下数据库结果的数据表

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

但我想根据每个名称或 ID 总结金额,我不知道 LINQ

4

1 回答 1

1

恐怕DataTable's Column-Expression不能SUM(Amount)OVER(PARTITION BY 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
于 2012-04-27T08:17:25.287 回答