Enumerable.ToList<TSource>(IEnumerable<TSource>) Methode

Definition

Erstellt eine List<T> aus einem 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)

Typparameter

TSource

Der Typ der Elemente von source.

Parameter

source
IEnumerable<TSource>

Der IEnumerable<T> zu erstellende Aus List<T> .

Gibt zurück

List<TSource>

A List<T> , das Elemente aus der Eingabesequenz enthält.

Ausnahmen

source ist null.

Beispiele

Im folgenden Codebeispiel wird veranschaulicht, wie Sie die ToList sofortige Abfrageauswertung erzwingen und ein List<T> Objekt zurückgeben, das die Abfrageergebnisse enthält.

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

Hinweise

Die ToList<TSource>(IEnumerable<TSource>) Methode erzwingt die sofortige Abfrageauswertung und gibt ein List<T> Objekt zurück, das die Abfrageergebnisse enthält. Sie können diese Methode an Ihre Abfrage anfügen, um eine zwischengespeicherte Kopie der Abfrageergebnisse abzurufen.

ToArray weist ein ähnliches Verhalten auf, gibt aber anstelle eines Arrays zurück List<T>.

Gilt für: