按顺序值对数据表进行分组
Group a datatable by sequential values
我有一个 table 有 2 列 clmAge
和 clmPrice
像
Age Price
1 , 100
2 , 100
3 , 150
4 , 150
5 , 100
6 , 100
7 , 100
不同年龄的价格可能相同,我需要适用于所有连续年龄且价格相同的最小和最大年龄。我需要对我的数据进行分组,这样我的结果如下:
1-2 , 100
3-4 , 150
5-7 , 100
试试这个
Imports System.Data
Module Module1
Sub Main()
Dim dt As New DataTable()
dt.Columns.Add("clmAge", GetType(Integer))
dt.Columns.Add("clmPrice", GetType(Integer))
dt.Rows.Add(New Object() {1, 100})
dt.Rows.Add(New Object() {2, 100})
dt.Rows.Add(New Object() {3, 150})
dt.Rows.Add(New Object() {4, 150})
dt.Rows.Add(New Object() {5, 100})
dt.Rows.Add(New Object() {6, 100})
dt.Rows.Add(New Object() {7, 100})
Dim oldPrice = 0
Dim groups As New List(Of Group)
Dim newGroup As Group = Nothing
For Each row As DataRow In dt.AsEnumerable()
If row("clmPrice") <> oldPrice Then
newGroup = New Group()
groups.Add(newGroup)
newGroup.minAge = row("clmAge")
newGroup.price = row("clmPrice")
End If
newGroup.maxAge = row("clmAge")
oldPrice = row("clmPrice")
Next row
End Sub
End Module
Public Class Group
Public minAge As Integer
Public maxAge As Integer
Public price As Integer
End Class
我有一个 table 有 2 列 clmAge
和 clmPrice
像
Age Price
1 , 100
2 , 100
3 , 150
4 , 150
5 , 100
6 , 100
7 , 100
不同年龄的价格可能相同,我需要适用于所有连续年龄且价格相同的最小和最大年龄。我需要对我的数据进行分组,这样我的结果如下:
1-2 , 100
3-4 , 150
5-7 , 100
试试这个
Imports System.Data
Module Module1
Sub Main()
Dim dt As New DataTable()
dt.Columns.Add("clmAge", GetType(Integer))
dt.Columns.Add("clmPrice", GetType(Integer))
dt.Rows.Add(New Object() {1, 100})
dt.Rows.Add(New Object() {2, 100})
dt.Rows.Add(New Object() {3, 150})
dt.Rows.Add(New Object() {4, 150})
dt.Rows.Add(New Object() {5, 100})
dt.Rows.Add(New Object() {6, 100})
dt.Rows.Add(New Object() {7, 100})
Dim oldPrice = 0
Dim groups As New List(Of Group)
Dim newGroup As Group = Nothing
For Each row As DataRow In dt.AsEnumerable()
If row("clmPrice") <> oldPrice Then
newGroup = New Group()
groups.Add(newGroup)
newGroup.minAge = row("clmAge")
newGroup.price = row("clmPrice")
End If
newGroup.maxAge = row("clmAge")
oldPrice = row("clmPrice")
Next row
End Sub
End Module
Public Class Group
Public minAge As Integer
Public maxAge As Integer
Public price As Integer
End Class