Enumerable.SelectMany Yöntem
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
Bir dizinin her öğesini bir IEnumerable<T> öğesine projeler ve sonuçta elde edilen dizileri tek bir sırayla düzleştirir.
Aşırı Yüklemeler
| Name | Description |
|---|---|
| SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) |
Bir dizinin her öğesini bir IEnumerable<T>öğesine projeler, sonuçta elde edilen dizileri tek bir sırayla düzleştirir ve bu dizideki her öğede bir sonuç seçici işlevi çağırır. |
| SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) |
Bir dizinin her öğesini bir IEnumerable<T>öğesine projeler, sonuçta elde edilen dizileri tek bir sırayla düzleştirir ve bu dizideki her öğede bir sonuç seçici işlevi çağırır. Her kaynak öğenin dizini, bu öğenin ara öngörülen biçiminde kullanılır. |
| SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) |
Bir dizinin her öğesini bir IEnumerable<T> öğesine projeler ve sonuçta elde edilen dizileri tek bir sırayla düzleştirir. |
| SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) |
Bir dizinin her öğesini bir IEnumerable<T>öğesine projeler ve sonuçta elde edilen dizileri tek bir sırayla düzleştirir. Her kaynak öğenin dizini, bu öğenin öngörülen biçiminde kullanılır. |
SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)
- Kaynak:
- SelectMany.cs
- Kaynak:
- SelectMany.cs
- Kaynak:
- SelectMany.cs
- Kaynak:
- SelectMany.cs
- Kaynak:
- SelectMany.cs
Bir dizinin her öğesini bir IEnumerable<T>öğesine projeler, sonuçta elde edilen dizileri tek bir sırayla düzleştirir ve bu dizideki her öğede bir sonuç seçici işlevi çağırır.
public:
generic <typename TSource, typename TCollection, typename TResult>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<TResult> ^ SelectMany(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, System::Collections::Generic::IEnumerable<TCollection> ^> ^ collectionSelector, Func<TSource, TCollection, TResult> ^ resultSelector);
public static System.Collections.Generic.IEnumerable<TResult> SelectMany<TSource,TCollection,TResult>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,System.Collections.Generic.IEnumerable<TCollection>> collectionSelector, Func<TSource,TCollection,TResult> resultSelector);
static member SelectMany : seq<'Source> * Func<'Source, seq<'Collection>> * Func<'Source, 'Collection, 'Result> -> seq<'Result>
<Extension()>
Public Function SelectMany(Of TSource, TCollection, TResult) (source As IEnumerable(Of TSource), collectionSelector As Func(Of TSource, IEnumerable(Of TCollection)), resultSelector As Func(Of TSource, TCollection, TResult)) As IEnumerable(Of TResult)
Tür Parametreleri
- TSource
öğelerinin sourcetürü.
- TCollection
tarafından collectionSelectortoplanan ara öğelerin türü.
- TResult
Sonuçta elde edilen dizinin öğelerinin türü.
Parametreler
- source
- IEnumerable<TSource>
Yansıtacak değer dizisi.
- collectionSelector
- Func<TSource,IEnumerable<TCollection>>
Giriş dizisinin her öğesine uygulanacak bir dönüştürme işlevi.
- resultSelector
- Func<TSource,TCollection,TResult>
Ara dizinin her öğesine uygulanacak bir dönüştürme işlevi.
Döndürülenler
IEnumerable<T> Öğeleri, öğesinde bire çok dönüştürme işlevini collectionSelectorsource çağırmanın ve ardından bu dizi öğelerinin ve karşılık gelen kaynak öğelerinin her birini bir sonuç öğesiyle eşlemenin sonucudur.
Özel durumlar
source veya collectionSelector veya resultSelector şeklindedir null.
Örnekler
Aşağıdaki kod örneğinde, bir dizi üzerinde bire çok yansıtma gerçekleştirmek ve kaynak dizideki her bir öğeyi son çağrısının SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)kapsamında tutmak için sonuç seçici işlevinin nasıl Select kullanılacağı gösterilmektedir.
class PetOwner
{
public string Name { get; set; }
public List<string> Pets { get; set; }
}
public static void SelectManyEx3()
{
PetOwner[] petOwners =
{ new PetOwner { Name="Higa",
Pets = new List<string>{ "Scruffy", "Sam" } },
new PetOwner { Name="Ashkenazi",
Pets = new List<string>{ "Walker", "Sugar" } },
new PetOwner { Name="Price",
Pets = new List<string>{ "Scratches", "Diesel" } },
new PetOwner { Name="Hines",
Pets = new List<string>{ "Dusty" } } };
// Project the pet owner's name and the pet's name.
var query =
petOwners
.SelectMany(petOwner => petOwner.Pets, (petOwner, petName) => new { petOwner, petName })
.Where(ownerAndPet => ownerAndPet.petName.StartsWith("S"))
.Select(ownerAndPet =>
new
{
Owner = ownerAndPet.petOwner.Name,
Pet = ownerAndPet.petName
}
);
// Print the results.
foreach (var obj in query)
{
Console.WriteLine(obj);
}
}
// This code produces the following output:
//
// {Owner=Higa, Pet=Scruffy}
// {Owner=Higa, Pet=Sam}
// {Owner=Ashkenazi, Pet=Sugar}
// {Owner=Price, Pet=Scratches}
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
' This code produces the following output:
'
' Owner=Higa, Pet=Scruffy
' Owner=Higa, Pet=Sam
' Owner=Ashkenazi, Pet=Sugar
' Owner=Price, Pet=Scratches
Açıklamalar
Bu yöntem ertelenen yürütme kullanılarak uygulanır. Hemen dönüş değeri, eylemi gerçekleştirmek için gereken tüm bilgileri depolayan bir nesnedir. Bu yöntemle temsil edilen sorgu, nesne doğrudan GetEnumerator yöntemini çağırarak veya C# içinde foreach veya Visual Basic'de For Each kullanılarak numaralandırılana kadar yürütülür.
yöntemi, SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) çağrısından sourceSelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)sonra oluşan sorgu mantığı için öğelerini kapsam içinde tutmanız gerektiğinde kullanışlıdır. Kod örneği için Örnek bölümüne bakın. türündeki nesnelerle türündeki TSource nesneler arasında çift yönlü bir ilişki varsa, yani türündeki TCollection bir nesne onu üreten nesneyi almak TCollection için bir özellik sağlarsa, bu aşırı yüklemesine TSourceihtiyacınız SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)yoktur. Bunun yerine kullanabilir ve nesnesi aracılığıyla nesneye SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>)TSource geri gidebilirsinizTCollection.
Sorgu ifadesi söz diziminde, ilk ifadeden sonra her from yan tümcesi (C#) veya From yan tümcesi (Visual Basic) SelectMany çağrısına çevrilir.
Ayrıca bkz.
Şunlara uygulanır
SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)
- Kaynak:
- SelectMany.cs
- Kaynak:
- SelectMany.cs
- Kaynak:
- SelectMany.cs
- Kaynak:
- SelectMany.cs
- Kaynak:
- SelectMany.cs
Bir dizinin her öğesini bir IEnumerable<T>öğesine projeler, sonuçta elde edilen dizileri tek bir sırayla düzleştirir ve bu dizideki her öğede bir sonuç seçici işlevi çağırır. Her kaynak öğenin dizini, bu öğenin ara öngörülen biçiminde kullanılır.
public:
generic <typename TSource, typename TCollection, typename TResult>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<TResult> ^ SelectMany(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, int, System::Collections::Generic::IEnumerable<TCollection> ^> ^ collectionSelector, Func<TSource, TCollection, TResult> ^ resultSelector);
public static System.Collections.Generic.IEnumerable<TResult> SelectMany<TSource,TCollection,TResult>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int,System.Collections.Generic.IEnumerable<TCollection>> collectionSelector, Func<TSource,TCollection,TResult> resultSelector);
static member SelectMany : seq<'Source> * Func<'Source, int, seq<'Collection>> * Func<'Source, 'Collection, 'Result> -> seq<'Result>
<Extension()>
Public Function SelectMany(Of TSource, TCollection, TResult) (source As IEnumerable(Of TSource), collectionSelector As Func(Of TSource, Integer, IEnumerable(Of TCollection)), resultSelector As Func(Of TSource, TCollection, TResult)) As IEnumerable(Of TResult)
Tür Parametreleri
- TSource
öğelerinin sourcetürü.
- TCollection
tarafından collectionSelectortoplanan ara öğelerin türü.
- TResult
Sonuçta elde edilen dizinin öğelerinin türü.
Parametreler
- source
- IEnumerable<TSource>
Yansıtacak değer dizisi.
- collectionSelector
- Func<TSource,Int32,IEnumerable<TCollection>>
Her kaynak öğeye uygulanacak bir dönüştürme işlevi; işlevinin ikinci parametresi, kaynak öğenin dizinini temsil eder.
- resultSelector
- Func<TSource,TCollection,TResult>
Ara dizinin her öğesine uygulanacak bir dönüştürme işlevi.
Döndürülenler
IEnumerable<T> Öğeleri, öğesinde bire çok dönüştürme işlevini collectionSelectorsource çağırmanın ve ardından bu dizi öğelerinin ve karşılık gelen kaynak öğelerinin her birini bir sonuç öğesiyle eşlemenin sonucudur.
Özel durumlar
source veya collectionSelector veya resultSelector şeklindedir null.
Açıklamalar
Bu yöntem ertelenen yürütme kullanılarak uygulanır. Hemen dönüş değeri, eylemi gerçekleştirmek için gereken tüm bilgileri depolayan bir nesnedir. Bu yöntemle temsil edilen sorgu, nesne doğrudan GetEnumerator yöntemini çağırarak veya C# içinde foreach veya Visual Basic'de For Each kullanılarak numaralandırılana kadar yürütülür.
yöntemi, SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) çağrısından sourceSelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)sonra oluşan sorgu mantığı için öğelerini kapsam içinde tutmanız gerektiğinde kullanışlıdır. Kod örneği için Örnek bölümüne bakın. türündeki nesnelerle türündeki TSource nesneler arasında çift yönlü bir ilişki varsa, yani türündeki TCollection bir nesne onu üreten nesneyi almak TCollection için bir özellik sağlarsa, bu aşırı yüklemesine TSourceihtiyacınız SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)yoktur. Bunun yerine kullanabilir ve nesnesi aracılığıyla nesneye SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>)TSource geri gidebilirsinizTCollection.
Şunlara uygulanır
SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>)
- Kaynak:
- SelectMany.cs
- Kaynak:
- SelectMany.cs
- Kaynak:
- SelectMany.cs
- Kaynak:
- SelectMany.cs
- Kaynak:
- SelectMany.cs
Bir dizinin her öğesini bir IEnumerable<T> öğesine projeler ve sonuçta elde edilen dizileri tek bir sırayla düzleştirir.
public:
generic <typename TSource, typename TResult>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<TResult> ^ SelectMany(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, System::Collections::Generic::IEnumerable<TResult> ^> ^ selector);
public static System.Collections.Generic.IEnumerable<TResult> SelectMany<TSource,TResult>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,System.Collections.Generic.IEnumerable<TResult>> selector);
static member SelectMany : seq<'Source> * Func<'Source, seq<'Result>> -> seq<'Result>
<Extension()>
Public Function SelectMany(Of TSource, TResult) (source As IEnumerable(Of TSource), selector As Func(Of TSource, IEnumerable(Of TResult))) As IEnumerable(Of TResult)
Tür Parametreleri
- TSource
öğelerinin sourcetürü.
- TResult
tarafından selectordöndürülen dizi öğelerinin türü.
Parametreler
- source
- IEnumerable<TSource>
Yansıtacak değer dizisi.
- selector
- Func<TSource,IEnumerable<TResult>>
Her öğeye uygulanacak bir dönüştürme işlevi.
Döndürülenler
IEnumerable<T> Öğeleri, giriş dizisinin her öğesinde bire çok dönüştürme işlevini çağırmanın sonucudur.
Özel durumlar
source veya selector şeklindedir null.
Örnekler
Aşağıdaki kod örneği, bir dizi üzerinde bire çok yansıtma gerçekleştirmek için nasıl kullanılacağını SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) gösterir.
class PetOwner
{
public string Name { get; set; }
public List<String> Pets { get; set; }
}
public static void SelectManyEx1()
{
PetOwner[] petOwners =
{ new PetOwner { Name="Higa, Sidney",
Pets = new List<string>{ "Scruffy", "Sam" } },
new PetOwner { Name="Ashkenazi, Ronen",
Pets = new List<string>{ "Walker", "Sugar" } },
new PetOwner { Name="Price, Vernette",
Pets = new List<string>{ "Scratches", "Diesel" } } };
// Query using SelectMany().
IEnumerable<string> query1 = petOwners.SelectMany(petOwner => petOwner.Pets);
Console.WriteLine("Using SelectMany():");
// Only one foreach loop is required to iterate
// through the results since it is a
// one-dimensional collection.
foreach (string pet in query1)
{
Console.WriteLine(pet);
}
// This code shows how to use Select()
// instead of SelectMany().
IEnumerable<List<String>> query2 =
petOwners.Select(petOwner => petOwner.Pets);
Console.WriteLine("\nUsing Select():");
// Notice that two foreach loops are required to
// iterate through the results
// because the query returns a collection of arrays.
foreach (List<String> petList in query2)
{
foreach (string pet in petList)
{
Console.WriteLine(pet);
}
Console.WriteLine();
}
}
/*
This code produces the following output:
Using SelectMany():
Scruffy
Sam
Walker
Sugar
Scratches
Diesel
Using Select():
Scruffy
Sam
Walker
Sugar
Scratches
Diesel
*/
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
' This code produces the following output:
'
' Using SelectMany():
' Scruffy
' Sam
' Walker
' Sugar
' Scratches
' Diesel
'
' Using Select():
' Scruffy
' Sam
' Walker
' Sugar
' Scratches
' Diesel
Açıklamalar
Bu yöntem ertelenen yürütme kullanılarak uygulanır. Hemen dönüş değeri, eylemi gerçekleştirmek için gereken tüm bilgileri depolayan bir nesnedir. Bu yöntemle temsil edilen sorgu, nesne doğrudan GetEnumerator yöntemini çağırarak veya C# içinde foreach veya Visual Basic'de For Each kullanılarak numaralandırılana kadar yürütülür.
SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) yöntemi giriş dizisini numaralandırır, her öğeyi bir ile eşlemek için bir IEnumerable<T>dönüştürme işlevi kullanır ve sonra bu tür IEnumerable<T> her nesnenin öğelerini numaralandırır ve verir. Diğer bir ifadeyle, öğesinin sourceselector her öğesi için çağrılır ve bir değer dizisi döndürülür.
SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) ardından bu iki boyutlu koleksiyon koleksiyonunu tek IEnumerable<T> boyutlu olarak düzleştirir ve döndürür. Örneğin, bir sorgu veritabanındaki her müşterinin siparişlerini (SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) türünde) almak için Order kullanırsa, sonuç C# dilinde IEnumerable<Order> veya Visual Basic IEnumerable(Of Order) türünde olur. Bunun yerine sorgu, siparişleri almak için Select kullanırsa, sipariş koleksiyonları koleksiyonu birleştirılmaz ve sonuç C# dilinde IEnumerable<List<Order>> türünde veya Visual Basic IEnumerable(Of List(Of Order)) türünde olur.
Sorgu ifadesi söz diziminde, ilk ifadeden sonra her from yan tümcesi (C#) veya From yan tümcesi (Visual Basic) SelectMany çağrısına çevrilir.
Ayrıca bkz.
Şunlara uygulanır
SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>)
- Kaynak:
- SelectMany.cs
- Kaynak:
- SelectMany.cs
- Kaynak:
- SelectMany.cs
- Kaynak:
- SelectMany.cs
- Kaynak:
- SelectMany.cs
Bir dizinin her öğesini bir IEnumerable<T>öğesine projeler ve sonuçta elde edilen dizileri tek bir sırayla düzleştirir. Her kaynak öğenin dizini, bu öğenin öngörülen biçiminde kullanılır.
public:
generic <typename TSource, typename TResult>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<TResult> ^ SelectMany(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, int, System::Collections::Generic::IEnumerable<TResult> ^> ^ selector);
public static System.Collections.Generic.IEnumerable<TResult> SelectMany<TSource,TResult>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int,System.Collections.Generic.IEnumerable<TResult>> selector);
static member SelectMany : seq<'Source> * Func<'Source, int, seq<'Result>> -> seq<'Result>
<Extension()>
Public Function SelectMany(Of TSource, TResult) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Integer, IEnumerable(Of TResult))) As IEnumerable(Of TResult)
Tür Parametreleri
- TSource
öğelerinin sourcetürü.
- TResult
tarafından selectordöndürülen dizi öğelerinin türü.
Parametreler
- source
- IEnumerable<TSource>
Yansıtacak değer dizisi.
- selector
- Func<TSource,Int32,IEnumerable<TResult>>
Her kaynak öğeye uygulanacak bir dönüştürme işlevi; işlevinin ikinci parametresi, kaynak öğenin dizinini temsil eder.
Döndürülenler
IEnumerable<T> Öğeleri, giriş dizisinin her öğesinde bire çok dönüştürme işlevini çağırmanın sonucudur.
Özel durumlar
source veya selector şeklindedir null.
Örnekler
Aşağıdaki kod örneği, bir dizi üzerinde bire çok yansıtma gerçekleştirmek ve her dış öğenin dizinini kullanmak için nasıl SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) kullanılacağını gösterir.
class PetOwner
{
public string Name { get; set; }
public List<string> Pets { get; set; }
}
public static void SelectManyEx2()
{
PetOwner[] petOwners =
{ new PetOwner { Name="Higa, Sidney",
Pets = new List<string>{ "Scruffy", "Sam" } },
new PetOwner { Name="Ashkenazi, Ronen",
Pets = new List<string>{ "Walker", "Sugar" } },
new PetOwner { Name="Price, Vernette",
Pets = new List<string>{ "Scratches", "Diesel" } },
new PetOwner { Name="Hines, Patrick",
Pets = new List<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.
IEnumerable<string> query =
petOwners.SelectMany((petOwner, index) =>
petOwner.Pets.Select(pet => index + pet));
foreach (string pet in query)
{
Console.WriteLine(pet);
}
}
// This code produces the following output:
//
// 0Scruffy
// 0Sam
// 1Walker
// 1Sugar
// 2Scratches
// 2Diesel
// 3Dusty
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
Açıklamalar
Bu yöntem ertelenen yürütme kullanılarak uygulanır. Hemen dönüş değeri, eylemi gerçekleştirmek için gereken tüm bilgileri depolayan bir nesnedir. Bu yöntemle temsil edilen sorgu, nesne doğrudan GetEnumerator yöntemini çağırarak veya C# içinde foreach veya Visual Basic'de For Each kullanılarak numaralandırılana kadar yürütülür.
SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) yöntemi giriş dizisini numaralandırır, her öğeyi bir ile eşlemek için bir IEnumerable<T>dönüştürme işlevi kullanır ve sonra bu tür IEnumerable<T> her nesnenin öğelerini numaralandırır ve verir. Diğer bir ifadeyle, öğesinin sourceselector her öğesi için çağrılır ve bir değer dizisi döndürülür.
SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) ardından bu iki boyutlu koleksiyon koleksiyonunu tek IEnumerable<T> boyutlu olarak düzleştirir ve döndürür. Örneğin, bir sorgu veritabanındaki her müşterinin siparişlerini (SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) türünde) almak için Order kullanırsa, sonuç C# dilinde IEnumerable<Order> veya Visual Basic IEnumerable(Of Order) türünde olur. Bunun yerine sorgu, siparişleri almak için Select kullanırsa, sipariş koleksiyonları koleksiyonu birleştirılmaz ve sonuç C# dilinde IEnumerable<List<Order>> türünde veya Visual Basic IEnumerable(Of List(Of Order)) türünde olur.
İşlenmek üzere öğeyi selector temsil eden ilk bağımsız değişken. için ikinci bağımsız değişken selector , kaynak dizideki bu öğenin sıfır tabanlı dizinini temsil eder. Öğeler bilinen bir sıradaysa ve belirli bir dizindeki bir öğeyle bir şey yapmak istiyorsanız, örneğin bu yararlı olabilir. Ayrıca, bir veya daha fazla öğenin dizinini almak istediğinizde de yararlı olabilir.