Enumerable.ToList<TSource>(IEnumerable<TSource>) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從 List<T>建立 IEnumerable<T>。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::List<TSource> ^ ToList(System::Collections::Generic::IEnumerable<TSource> ^ source);
public static System.Collections.Generic.List<TSource> ToList<TSource>(this System.Collections.Generic.IEnumerable<TSource> source);
static member ToList : seq<'Source> -> System.Collections.Generic.List<'Source>
<Extension()>
Public Function ToList(Of TSource) (source As IEnumerable(Of TSource)) As List(Of TSource)
類型參數
- TSource
元素 source的類型。
參數
- source
- IEnumerable<TSource>
要 IEnumerable<T> 創造一個 List<T> 來源。
傳回
List<TSource>
包含輸入序列元素的 A List<T> 。
例外狀況
source 是 null。
範例
以下程式碼範例示範如何強制 ToList 立即查詢評估並回傳包含查詢結果的 a List<T> 。
string[] fruits = { "apple", "passionfruit", "banana", "mango",
"orange", "blueberry", "grape", "strawberry" };
List<int> lengths = fruits.Select(fruit => fruit.Length).ToList();
foreach (int length in lengths)
{
Console.WriteLine(length);
}
/*
This code produces the following output:
5
12
6
5
6
9
5
10
*/
' Create an array of strings.
Dim fruits() As String =
{"apple", "passionfruit", "banana", "mango",
"orange", "blueberry", "grape", "strawberry"}
' Project the length of each string and
' put the length values into a List object.
Dim lengths As List(Of Integer) =
fruits _
.Select(Function(fruit) fruit.Length) _
.ToList()
' Display the results.
Dim output As New System.Text.StringBuilder
For Each length As Integer In lengths
output.AppendLine(length)
Next
Console.WriteLine(output.ToString())
' This code produces the following output:
'
' 5
' 12
' 6
' 5
' 6
' 9
' 5
' 10
備註
此 ToList<TSource>(IEnumerable<TSource>) 方法強制立即查詢評估,並回傳包含查詢結果的 a List<T> 。 你可以將此方法附加到查詢中,以取得查詢結果的快取副本。
ToArray 有類似行為,但回傳的是陣列而非 List<T>。