本文整理匯總了VB.NET中System.Linq.Enumerable.SelectMany方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Enumerable.SelectMany方法的具體用法?VB.NET Enumerable.SelectMany怎麽用?VB.NET Enumerable.SelectMany使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。
在下文中一共展示了Enumerable.SelectMany方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: SelectManyEx3
Structure PetOwner
Public Name As String
Public Pets() As String
End Structure
Sub SelectManyEx3()
' Create an array of PetOwner objects.
Dim petOwners() As PetOwner =
{New PetOwner With
{.Name = "Higa", .Pets = New String() {"Scruffy", "Sam"}},
New PetOwner With
{.Name = "Ashkenazi", .Pets = New String() {"Walker", "Sugar"}},
New PetOwner With
{.Name = "Price", .Pets = New String() {"Scratches", "Diesel"}},
New PetOwner With
{.Name = "Hines", .Pets = New String() {"Dusty"}}}
' Project an anonymous type that consists of
' the owner's name and the pet's name (string).
Dim query =
petOwners _
.SelectMany(
Function(petOwner) petOwner.Pets,
Function(petOwner, petName) New With {petOwner, petName}) _
.Where(Function(ownerAndPet) ownerAndPet.petName.StartsWith("S")) _
.Select(Function(ownerAndPet) _
New With {.Owner = ownerAndPet.petOwner.Name,
.Pet = ownerAndPet.petName
})
Dim output As New System.Text.StringBuilder
For Each obj In query
output.AppendLine(String.Format("Owner={0}, Pet={1}", obj.Owner, obj.Pet))
Next
' Display the output.
Console.WriteLine(output.ToString())
End Sub
輸出:
Owner=Higa, Pet=Scruffy Owner=Higa, Pet=Sam Owner=Ashkenazi, Pet=Sugar Owner=Price, Pet=Scratches
示例2: SelectManyEx1
Structure PetOwner
Public Name As String
Public Pets() As String
End Structure
Sub SelectManyEx1()
' Create an array of PetOwner objects.
Dim petOwners() As PetOwner =
{New PetOwner With
{.Name = "Higa, Sidney", .Pets = New String() {"Scruffy", "Sam"}},
New PetOwner With
{.Name = "Ashkenazi, Ronen", .Pets = New String() {"Walker", "Sugar"}},
New PetOwner With
{.Name = "Price, Vernette", .Pets = New String() {"Scratches", "Diesel"}}}
' Call SelectMany() to gather all pets into a "flat" sequence.
Dim query1 As IEnumerable(Of String) =
petOwners.SelectMany(Function(petOwner) petOwner.Pets)
Dim output As New System.Text.StringBuilder("Using SelectMany():" & vbCrLf)
' Only one foreach loop is required to iterate through
' the results because it is a one-dimensional collection.
For Each pet As String In query1
output.AppendLine(pet)
Next
' This code demonstrates how to use Select() instead
' of SelectMany() to get the same result.
Dim query2 As IEnumerable(Of String()) =
petOwners.Select(Function(petOwner) petOwner.Pets)
output.AppendLine(vbCrLf & "Using Select():")
' Notice that two foreach loops are required to iterate through
' the results because the query returns a collection of arrays.
For Each petArray() As String In query2
For Each pet As String In petArray
output.AppendLine(pet)
Next
Next
' Display the output.
Console.WriteLine(output.ToString())
End Sub
輸出:
Using SelectMany(): Scruffy Sam Walker Sugar Scratches Diesel Using Select(): Scruffy Sam Walker Sugar Scratches Diesel
示例3: SelectManyEx2
Structure PetOwner
Public Name As String
Public Pets() As String
End Structure
Sub SelectManyEx2()
' Create an array of PetOwner objects.
Dim petOwners() As PetOwner =
{New PetOwner With
{.Name = "Higa, Sidney", .Pets = New String() {"Scruffy", "Sam"}},
New PetOwner With
{.Name = "Ashkenazi, Ronen", .Pets = New String() {"Walker", "Sugar"}},
New PetOwner With
{.Name = "Price, Vernette", .Pets = New String() {"Scratches", "Diesel"}},
New PetOwner With
{.Name = "Hines, Patrick", .Pets = New String() {"Dusty"}}}
' Project the items in the array by appending the index
' of each PetOwner to each pet's name in that petOwner's
' array of pets.
Dim query As IEnumerable(Of String) =
petOwners.SelectMany(Function(petOwner, index) _
petOwner.Pets.Select(Function(pet) _
index.ToString() + pet))
Dim output As New System.Text.StringBuilder
For Each pet As String In query
output.AppendLine(pet)
Next
' Display the output.
Console.WriteLine(output.ToString())
End Sub