Enumerable.Concat<TSource> 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
串連兩個序列。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<TSource> ^ Concat(System::Collections::Generic::IEnumerable<TSource> ^ first, System::Collections::Generic::IEnumerable<TSource> ^ second);
public static System.Collections.Generic.IEnumerable<TSource> Concat<TSource>(this System.Collections.Generic.IEnumerable<TSource> first, System.Collections.Generic.IEnumerable<TSource> second);
static member Concat : seq<'Source> * seq<'Source> -> seq<'Source>
<Extension()>
Public Function Concat(Of TSource) (first As IEnumerable(Of TSource), second As IEnumerable(Of TSource)) As IEnumerable(Of TSource)
類型參數
- TSource
輸入序列元素的類型。
參數
- first
- IEnumerable<TSource>
第一個串接的序列。
- second
- IEnumerable<TSource>
該序列將與第一個序列連接起來。
傳回
包含 IEnumerable<T> 兩個輸入序列的串接元素。
例外狀況
first 或 second 為 null。
範例
以下程式碼範例示範如何用 Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) 來串接兩個序列。
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
static Pet[] GetCats()
{
Pet[] cats = { new Pet { Name="Barley", Age=8 },
new Pet { Name="Boots", Age=4 },
new Pet { Name="Whiskers", Age=1 } };
return cats;
}
static Pet[] GetDogs()
{
Pet[] dogs = { new Pet { Name="Bounder", Age=3 },
new Pet { Name="Snoopy", Age=14 },
new Pet { Name="Fido", Age=9 } };
return dogs;
}
public static void ConcatEx1()
{
Pet[] cats = GetCats();
Pet[] dogs = GetDogs();
IEnumerable<string> query =
cats.Select(cat => cat.Name).Concat(dogs.Select(dog => dog.Name));
foreach (string name in query)
{
Console.WriteLine(name);
}
}
// This code produces the following output:
//
// Barley
// Boots
// Whiskers
// Bounder
// Snoopy
// Fido
Structure Pet
Public Name As String
Public Age As Integer
End Structure
' Returns an array of Pet objects.
Function GetCats() As Pet()
Dim cats() As Pet = {New Pet With {.Name = "Barley", .Age = 8},
New Pet With {.Name = "Boots", .Age = 4},
New Pet With {.Name = "Whiskers", .Age = 1}}
Return cats
End Function
' Returns an array of Pet objects.
Function GetDogs() As Pet()
Dim dogs() As Pet = {New Pet With {.Name = "Bounder", .Age = 3},
New Pet With {.Name = "Snoopy", .Age = 14},
New Pet With {.Name = "Fido", .Age = 9}}
Return dogs
End Function
Sub ConcatEx1()
' Create two arrays of Pet objects.
Dim cats() As Pet = GetCats()
Dim dogs() As Pet = GetDogs()
' Project the Name of each cat and concatenate
' the collection of cat name strings with a collection
' of dog name strings.
Dim query As IEnumerable(Of String) =
cats _
.Select(Function(cat) cat.Name) _
.Concat(dogs.Select(Function(dog) dog.Name))
Dim output As New System.Text.StringBuilder
For Each name As String In query
output.AppendLine(name)
Next
' Display the output.
Console.WriteLine(output.ToString())
End Sub
' This code produces the following output:
'
' Barley
' Boots
' Whiskers
' Bounder
' Snoopy
' Fido
另一種串接兩個序列的方法是構造一個序列集合,例如陣列,然後套用該 SelectMany 方法,將恆等選擇函數傳給該序列。 以下範例展示了 的 SelectMany這種用法。
Pet[] cats = GetCats();
Pet[] dogs = GetDogs();
IEnumerable<string> query =
new[] { cats.Select(cat => cat.Name), dogs.Select(dog => dog.Name) }
.SelectMany(name => name);
foreach (string name in query)
{
Console.WriteLine(name);
}
// This code produces the following output:
//
// Barley
// Boots
// Whiskers
// Bounder
// Snoopy
// Fido
' Create two arrays of Pet objects.
Dim cats() As Pet = GetCats()
Dim dogs() As Pet = GetDogs()
' Create an IEnumerable collection that contains two elements.
' Each element is an array of Pet objects.
Dim animals() As IEnumerable(Of Pet) = {cats, dogs}
Dim query As IEnumerable(Of String) =
(animals.SelectMany(Function(pets) _
pets.Select(Function(pet) pet.Name)))
Dim output As New System.Text.StringBuilder
For Each name As String In query
output.AppendLine(name)
Next
' Display the output.
Console.WriteLine(output.ToString())
' This code produces the following output:
'
' Barley
' Boots
' Whiskers
' Bounder
' Snoopy
' Fido
備註
此方法透過延遲執行實現。 立即回傳值是一個物件,儲存執行動作所需的所有資訊。 此方法所代表的查詢,直到物件被枚舉,無論是直接呼叫其 GetEnumerator 方法,或是使用 C# 中的 foreach,或在 Visual Basic 中使用 For Each,才會執行。
此 Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) 方法與 Method Union 不同之處在於,該 Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) 方法回傳輸入序列中所有原始元素。 該 Union 方法僅回傳唯一元素。