本文整理匯總了VB.NET中System.Linq.Queryable.GroupBy方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Queryable.GroupBy方法的具體用法?VB.NET Queryable.GroupBy怎麽用?VB.NET Queryable.GroupBy使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。
在下文中一共展示了Queryable.GroupBy方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: List
Structure Pet
Public Name As String
Public Age As Double
End Structure
Shared Sub GroupByEx4()
' Create a list of pets.
Dim petsList As New List(Of Pet)(New Pet() { _
New Pet With {.Name = "Barley", .Age = 8.3}, _
New Pet With {.Name = "Boots", .Age = 4.9}, _
New Pet With {.Name = "Whiskers", .Age = 1.5}, _
New Pet With {.Name = "Daisy", .Age = 4.3}})
' Group Pet.Age valuesby the Math.Floor of the age.
' Then project an anonymous type from each group
' that consists of the key, the count of the group's
' elements, and the minimum and maximum age in the group.
Dim query = petsList.AsQueryable().GroupBy( _
Function(pet) Math.Floor(pet.Age), _
Function(pet) pet.Age, _
Function(baseAge, ages) New With { _
.Key = baseAge, _
.Count = ages.Count(), _
.Min = ages.Min(), _
.Max = ages.Max() _
})
Dim output As New System.Text.StringBuilder
' Iterate over each anonymous type.
For Each result In query
output.AppendLine(vbCrLf & "Age group: " & result.Key)
output.AppendLine("Number of pets with this age: " & result.Count)
output.AppendLine("Minimum age: " & result.Min)
output.AppendLine("Maximum age: " & result.Max)
Next
' Display the output.
MsgBox(output.ToString())
' This code produces the following output:
' Age group: 8
' Number of pets with this age: 1
' Minimum age: 8.3
' Maximum age: 8.3
' Age group: 4
' Number of pets with this age: 2
' Minimum age: 4.3
' Maximum age: 4.9
' Age group: 1
' Number of pets with this age: 1
' Minimum age: 1.5
' Maximum age: 1.5
End Sub
示例2: List
Structure Pet
Public Name As String
Public Age As Integer
End Structure
Shared Sub GroupByEx2()
' Create a list of Pet objects.
Dim pets As New List(Of Pet)(New Pet() { _
New Pet With {.Name = "Barley", .Age = 8}, _
New Pet With {.Name = "Boots", .Age = 4}, _
New Pet With {.Name = "Whiskers", .Age = 1}, _
New Pet With {.Name = "Daisy", .Age = 4}})
' Group the pets using Pet.Age as the key.
' Use Pet.Name as the value for each entry.
Dim query As IEnumerable(Of IGrouping(Of Integer, String)) = _
pets.AsQueryable().GroupBy(Function(pet) pet.Age, Function(pet) pet.Name)
Dim output As New System.Text.StringBuilder
' Iterate over each IGrouping in the collection.
For Each petGroup As IGrouping(Of Integer, String) In query
' Print the key value of the IGrouping.
output.AppendLine(petGroup.Key)
' Iterate over each value in the
' IGrouping and print the value.
For Each name As String In petGroup
output.AppendLine(String.Format(" {0}", name))
Next
Next
' Display the output.
MsgBox(output.ToString())
End Sub
輸出:
8 Barley 4 Boots Daisy 1 Whiskers
示例3: List
Structure Pet
Public Name As String
Public Age As Double
End Structure
Shared Sub GroupByEx3()
' Create a list of pets.
Dim petsList As New List(Of Pet)(New Pet() { _
New Pet With {.Name = "Barley", .Age = 8.3}, _
New Pet With {.Name = "Boots", .Age = 4.9}, _
New Pet With {.Name = "Whiskers", .Age = 1.5}, _
New Pet With {.Name = "Daisy", .Age = 4.3}})
' Group Pet objects by the Math.Floor of their age.
' Then project an anonymous type from each group
' that consists of the key, the count of the group's
' elements, and the minimum and maximum age in the group.
Dim query = petsList.AsQueryable().GroupBy( _
Function(pet) Math.Floor(pet.Age), _
Function(age, pets) New With { _
.Key = age, _
.Count = pets.Count(), _
.Min = pets.Min(Function(pet) pet.Age), _
.Max = pets.Max(Function(pet) pet.Age) _
})
Dim output As New System.Text.StringBuilder
' Iterate over each anonymous type.
For Each result In query
output.AppendLine(vbCrLf & "Age group: " & result.Key)
output.AppendLine("Number of pets with this age: " & result.Count)
output.AppendLine("Minimum age: " & result.Min)
output.AppendLine("Maximum age: " & result.Max)
Next
' Display the output.
MsgBox(output.ToString())
' This code produces the following output:
' Age group: 8
' Number of pets with this age: 1
' Minimum age: 8.3
' Maximum age: 8.3
' Age group: 4
' Number of pets with this age: 2
' Minimum age: 4.3
' Maximum age: 4.9
' Age group: 1
' Number of pets with this age: 1
' Minimum age: 1.5
' Maximum age: 1.5
End Sub
示例4: List
Structure Pet
Public Name As String
Public Age As Integer
End Structure
Shared Sub GroupByEx1()
' Create a list of Pet objects.
Dim pets As New List(Of Pet)(New Pet() { _
New Pet With {.Name = "Barley", .Age = 8}, _
New Pet With {.Name = "Boots", .Age = 4}, _
New Pet With {.Name = "Whiskers", .Age = 1}, _
New Pet With {.Name = "Daisy", .Age = 4}})
' Group the pets using Pet.Age as the key.
' Use Pet.Name as the value for each entry.
Dim query = pets.AsQueryable().GroupBy(Function(pet) pet.Age)
Dim output As New System.Text.StringBuilder
' Iterate over each IGrouping in the collection.
For Each ageGroup In query
output.AppendFormat("Age group: {0} Number of pets: {1}{2}", ageGroup.Key, ageGroup.Count(), vbCrLf)
Next
' Display the output.
MsgBox(output.ToString())
End Sub
輸出:
Age group: 8 Number of pets: 1 Age group: 4 Number of pets: 2 Age group: 1 Number of pets: 1