vb datatable group 多列分组示例
时间: 2023-08-02 09:22:43 浏览: 194
datatables 示例
可以使用`Group By`子句来对`DataTable`进行多列分组。以下是一个示例代码:
```vb
Imports System.Data
Module Module1
Sub Main()
Dim people As New DataTable()
people.Columns.Add("Name", GetType(String))
people.Columns.Add("Age", GetType(Integer))
people.Columns.Add("City", GetType(String))
people.Rows.Add("Alice", 20, "Beijing")
people.Rows.Add("Bob", 30, "Shanghai")
people.Rows.Add("Charlie", 20, "Beijing")
people.Rows.Add("David", 40, "Shanghai")
Dim groupedPeople = From p In people.AsEnumerable()
Group By key = New With {Key .Age = p.Field(Of Integer)("Age"), Key .City = p.Field(Of String)("City")} Into Group
Select Age = key.Age, City = key.City, People = Group
For Each group In groupedPeople
Console.WriteLine("People with age {0} and in city {1}:", group.Age, group.City)
For Each person In group.People
Console.WriteLine(person.Field(Of String)("Name"))
Next
Console.WriteLine()
Next
End Sub
End Module
```
在上面的示例中,我们首先创建了一个`DataTable`对象,包含三个列`Name`、`Age`和`City`。然后我们向表中添加了四个行数据,使用`Group By`子句进行多列分组,最终的输出结果会按照`Age`和`City`两列进行分组,并输出每个分组中的人员名单。
阅读全文