0

如何在 LINQ 中使用 DataTable 在 vb 代码(dot.net v4.0)中执行组并对组求和?

在下面的示例中,我需要添加 group by GroupNameProductName然后执行 sum on QTY。列、顺序和位置应该保持在示例中,我只需要添加组和总和。格式应保持不变(使用 e("FieldName") 获取行)。

Dim ordersTable As DataTable = _dsProd.Tables("tblProductSummary")
Dim query =
         (From e In ordersTable
          Where (e("Type").ToString() = "1" Or IsDBNull(e("Type")))
          Order By e("GroupSortOrder") Ascending, e("ProductName")
          Select
            GroupName = e("GroupName"),
            ProductName = e("ProductName"),
            QTY = e("QTY"),
            Type= e("Type")
          )
4

1 回答 1

0
Dim query =
         (From e In ordersTable
          Where (e("Type").ToString() = "1" Or IsDBNull(e("Type")))
          Order By e("GroupSortOrder") Ascending, e("ProductName")
          Group e By Key = New With {
              .ProductName = e("ProductName"),
              .GroupName = e("GroupName")
          } Into Group
          Select New With {
              .ProductName = Key.ProductName,
              .GroupName = Key.GroupName,
              .Sum = Group.Sum(Function(x) x("QTY"))
          })
于 2014-02-02T17:45:27.503 回答