我有一个看起来像这样的LINQ表达式
Dim DataRowDueTo = From row In DataTableDueTo.AsEnumerable
Where row.Field(Of UInt32)("Transaction") = 1 And {"A", "B", "C"}.Contains(row.Field(Of String)("Module"))
Group By x = New With {Key .CustID = row.Field(Of UInt32)("CustID"), Key .Module = row.Field(Of String)("Module")} Into g = Group
Select New With {
.CustID = x.CustID,
.Module = x.Module,
.Sum= g.Sum(Function(r) r.Field(Of Decimal)("Amount"))
}
''DataTableDueToFinal is a datatable i have precreated with columns (CustID,Module and Sum)
For Each grp In DataRowDueTo
DataTableDueToFinal.Rows.Add(grp.CustID, grp.Module, grp.Sum)
Next
基本上,我有一组数据(来自DataTable),其中GROUP BY
2列(CustID
和Module
)使用Where
由{{1} }和TransactionSource
。
产生的结果看起来像这样
Module
我的问题是,我试图使用不同的方法来重现相同的结果,但是我无法获得预期的结果。
+--------+--------+-----+
| CustID | Module | Sum |
+--------+--------+-----+
| 1 | A | 50 |
+--------+--------+-----+
| 1 | B | 45 |
+--------+--------+-----+
| 2 | A | 23 |
+--------+--------+-----+
| 3 | A | 234 |
+--------+--------+-----+
| 3 | B | 76 |
+--------+--------+-----+
谁能指出我哪里出了问题?谢谢您的时间和考虑。
答案 0 :(得分:1)
看起来像这样:
Dim DataRowDueTo = DataTableDueTo.AsEnumerable().Where(Function(w) w.Field(Of UInt32)("Transaction") = 1 And {"A", "B", "C"}.Contains(w.Field(Of String)("Module"))) _
.GroupBy(Function(row) New With {KEY .CustID = row.Field(Of UInt32)("CustID"),Key .Module = row.Field(Of String)("Module")})
''DataTableDueToFinal is a datatable i have precreated with columns (CustID,Module and Sum)
For Each grp In DataRowDueTo
DataTableDueToFinal.Rows.Add(grp.Key.CustID, grp.Key.Module, grp.Sum(Function(row) row.Field(Of Decimal)("Amount")))
Next