本文整理匯總了VB.NET中System.Linq.Queryable.SelectMany方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Queryable.SelectMany方法的具體用法?VB.NET Queryable.SelectMany怎麽用?VB.NET Queryable.SelectMany使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。
在下文中一共展示了Queryable.SelectMany方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: List
Structure PetOwner
Public Name As String
Public Pets As List(Of Pet)
End Structure
Structure Pet
Public Name As String
Public Breed As String
End Structure
Shared Sub SelectManyEx3()
Dim petOwners() As PetOwner = _
{New PetOwner With {.Name = "Higa", _
.Pets = New List(Of Pet)(New Pet() { _
New Pet With {.Name = "Scruffy", .Breed = "Poodle"}, _
New Pet With {.Name = "Sam", .Breed = "Hound"}})}, _
New PetOwner With {.Name = "Ashkenazi", _
.Pets = New List(Of Pet)(New Pet() { _
New Pet With {.Name = "Walker", .Breed = "Collie"}, _
New Pet With {.Name = "Sugar", .Breed = "Poodle"}})}, _
New PetOwner With {.Name = "Price", _
.Pets = New List(Of Pet)(New Pet() { _
New Pet With {.Name = "Scratches", .Breed = "Dachshund"}, _
New Pet With {.Name = "Diesel", .Breed = "Collie"}})}, _
New PetOwner With {.Name = "Hines", _
.Pets = New List(Of Pet)(New Pet() { _
New Pet With {.Name = "Dusty", .Breed = "Collie"}})} _
}
' This query demonstrates how to obtain a sequence of
' the names of all the pets whose breed is "Collie", while
' keeping an association with the owner that owns the pet.
Dim query = petOwners.AsQueryable() _
.SelectMany(Function(owner) owner.Pets, _
Function(owner, pet) New With {owner, pet}) _
.Where(Function(ownerAndPet) ownerAndPet.pet.Breed = "Collie") _
.Select(Function(ownerAndPet) New With { _
.Owner = ownerAndPet.owner.Name, _
.Pet = ownerAndPet.pet.Name})
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.
MsgBox(output.ToString())
End Sub
輸出:
Owner=Ashkenazi, Pet=Walker Owner=Price, Pet=Diesel Owner=Hines, Pet=Dusty
示例2: Pets
Structure PetOwner
Public Name As String
Public Pets() As String
End Structure
Shared Sub SelectManyEx2()
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"}}}
' For each PetOwner element in the source array,
' project a sequence of strings where each string
' consists of the index of the PetOwner element in the
' source array and the name of each pet in PetOwner.Pets.
Dim query As IEnumerable(Of String) = _
petOwners.AsQueryable() _
.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.
MsgBox(output.ToString())
End Sub
輸出:
0Scruffy 0Sam 1Walker 1Sugar 2Scratches 2Diesel 3Dusty
示例3: Pets
Structure PetOwner
Public Name As String
Public Pets() As String
End Structure
Shared Sub SelectManyEx1()
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"}}}
' Query using SelectMany().
Dim query1 As IEnumerable(Of String) = _
petOwners.AsQueryable().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 shows how to use Select() instead of SelectMany().
Dim query2 As IEnumerable(Of String()) = _
petOwners.AsQueryable().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.
MsgBox(output.ToString())
End Sub
輸出:
Using SelectMany(): Scruffy Sam Walker Sugar Scratches Diesel Using Select(): Scruffy Sam Walker Sugar Scratches Diesel