1

私はデータベースから次の結果を持つ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     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に対して金額を合計したい

4

1 に答える 1

1

DataTable's 列式ができないのではないかと思い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 に答える