1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Datatableの明細行をLINQで集計する例

Posted at

概要

発注伝票データの明細行に対して、伝票番号単位の金額集計をセットする処理を行う機会があったのでやってみた。

VB.NET
Dim totalGenka As Decimal = 0
Dim totalBaika As Decimal = 0

'dtには伝票明細データが格納されているとする

Dim dv As DataView
dv = New DataView(dt)

'伝票番号でマージする
Dim dtDen As DataTable = dv.ToTable(True, "伝票番号")
dv.Dispose()

'伝票番号分ループする
For i As Integer = 0 To dtDen.Rows.Count - 1

    '伝票単位の原価金額合計,売価金額合計を集計する
    totalGenka = 0
    totalBaika = 0

    'LINQを使う
    Dim lpCnt As Integer = i
    Dim query = From dr In dt.AsEnumerable
                Where dr("伝票番号") = dtDen.Rows(lpCnt).Item("伝票番号")
                Select dr

    '出荷数に対する合計を取得
    totalGenka = query.Sum(Function(dr) dr("原価金額") * dr("出荷数"))
    totalBaika = query.Sum(Function(dr) dr("売価金額") * dr("出荷数"))

    '明細行分ループする
    For m As Integer = 0 To dtMeisai.Rows.Count - 1
        '伝票明細の加工処理
    Next

Next
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?