当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET Enumerable.GroupBy方法代码示例

本文整理汇总了VB.NET中System.Linq.Enumerable.GroupBy方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Enumerable.GroupBy方法的具体用法?VB.NET Enumerable.GroupBy怎么用?VB.NET Enumerable.GroupBy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。


在下文中一共展示了Enumerable.GroupBy方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。

示例1: List

Structure Pet
    Public Name As String
    Public Age As Double
End Structure

Public 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 values by 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.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 in this age group: " & result.Count)
        output.AppendLine("Minimum age: " & result.Min)
        output.AppendLine("Maximum age: " & result.Max)
    Next

    ' Display the output.
    Console.WriteLine(output.ToString)
End Sub
开发者ID:VB.NET开发者,项目名称:System.Linq,代码行数:39,代码来源:Enumerable.GroupBy

输出:

Age group: 8
Number of pets in this age group: 1
Minimum age: 8.3
Maximum age: 8.3

Age group: 4
Number of pets in this age group: 2
Minimum age: 4.3
Maximum age: 4.9

Age group: 1
Number of pets in this age group: 1
Minimum age: 1.5
Maximum age: 1.5

示例2: GroupByEx1

Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

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 Age as the key 
    ' and selecting only the pet's Name for each value.
    Dim query As IEnumerable(Of IGrouping(Of Integer, String)) =
pets.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("  " & name)
        Next
    Next

    ' Display the output.
    Console.WriteLine(output.ToString)
End Sub
开发者ID:VB.NET开发者,项目名称:System.Linq,代码行数:33,代码来源:Enumerable.GroupBy

输出:

8
Barley
4
Boots
Daisy
1
Whiskers

示例3:

Dim query =
From pet In pets
Group pet.Name By Age = pet.Age Into ageGroup = Group
开发者ID:VB.NET开发者,项目名称:System.Linq,代码行数:3,代码来源:Enumerable.GroupBy

示例4: List

Structure Pet
    Public Name As String
    Public Age As Double
End Structure

Public 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.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 in this age group: " & result.Count)
        output.AppendLine("Minimum age: " & result.Min)
        output.AppendLine("Maximum age: " & result.Max)
    Next

    ' Display the output.
    Console.WriteLine(output.ToString)
End Sub
开发者ID:VB.NET开发者,项目名称:System.Linq,代码行数:38,代码来源:Enumerable.GroupBy

输出:

Age group: 8
Number of pets in this age group: 1
Minimum age: 8.3
Maximum age: 8.3

Age group: 4
Number of pets in this age group: 2
Minimum age: 4.3
Maximum age: 4.9

Age group: 1
Number of pets in this age group: 1
Minimum age: 1.5
Maximum age: 1.5


注:本文中的System.Linq.Enumerable.GroupBy方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。