0
示例方案VB.NET濾波/結合下面的字段的數據庫分組的DataTable
我有一個表: - 的SerialNo,GroupNo,說明,數量。此刻,我周圍已經從ADO.NET數據集填充,我加入到域列表如下一個DataTable循環...
' Gets the items from the database and created a DataSet
' The DataSet has a named DataTable called MyTable
ds = GetItems
' Item is an model in my MVC project
Dim Item As Item
' I am creating a List of items...
i As List(Of Item)
For Each row As DataRow In ds.Tables("MyTable").Rows
Item = New Item() With {
.SerialNo = If(Not IsDBNull(row("SerialNo")), CInt(row("SerialNo")), 0),
.GroupNo = If(Not IsDBNull(row("GroupNo")), CStr(row("GroupNo")), ""),
.Description = If(Not IsDBNull(row("Description")), CStr(row("Description")), ""),
.Quantity = If(Not IsDBNull(row("Quantity")), CInt(row("Quantity")), 0)
}
ai.Add(Item)
Next
要求
,而不是領我希望每一行都能得到每個GroupNo的第一次出現,並將這個結果返回給List。例如...
- 的SerialNo = 1 GroupNo = 1所描述=項A數量= 100
- 的SerialNo = 2 GroupNo = 1所描述= B項數量= 100
- 的SerialNo = 3 GroupNo = 1所說明= C項數量= 100
- 的SerialNo = 4 GroupNo = 2描述=項目d數量= 100
- 的SerialNo = 5 GroupNo = 2描述= E項數量= 100
- 的SerialNo = 6 GroupNo = 3描述=項目F數量= 100
...實際上應該被修飾以返回...
- 的SerialNo = 1 GroupNo = 1所描述=項A數量= 100
- 的SerialNo = 4 GroupNo = 2描述=項目d量= 100
- 的SerialNo = 6 GroupNo = 3描述=項目F數量= 100
我使用Visual Studio 2010(VB.NET)與.NET 4.0。
我試圖研究各種方式,但我要麼試圖提取所有4列似乎並沒有正確組合。注意:我不想修改查詢以僅返回數據的子集。我需要用代碼過濾/分組。
完美,謝謝。 – 2012-07-17 11:23:58