Enumerable.ToList<TSource>(IEnumerable<TSource>) メソッド
定義
IEnumerable<T> から List<T> を作成します。Creates a List<T> from an 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
の要素の型。The type of the elements of source
.
パラメーター
- source
- IEnumerable<TSource>
IEnumerable<T> の作成元の List<T>。The IEnumerable<T> to create a List<T> from.
戻り値
- List<TSource>
入力シーケンスの要素を含む List<T>。A List<T> that contains elements from the input sequence.
例外
source
が null
です。source
is null
.
例
次のコード例では、を使用して ToList クエリの即時評価を強制的に実行し、クエリ結果を含むを返す方法を示し List<T> ます。The following code example demonstrates how to use ToList to force immediate query evaluation and return a List<T> that contains the query results.
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>) クエリの即時評価を強制的に実行し、 List<T> クエリ結果を含むを返します。The ToList<TSource>(IEnumerable<TSource>) method forces immediate query evaluation and returns a List<T> that contains the query results. クエリ結果のキャッシュされたコピーを取得するために、このメソッドをクエリに追加することができます。You can append this method to your query in order to obtain a cached copy of the query results.
ToArray は同様の動作を持ちますが、ではなく配列を返し List<T> ます。ToArray has similar behavior but returns an array instead of a List<T>.