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

Definicja

Tworzy obiekt na List<T> podstawie elementu 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)

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IEnumerable<TSource>

Element IEnumerable<T> do utworzenia elementu List<T> na podstawie.

Zwraca

List<TSource>

Element List<T> zawierający elementy z sekwencji danych wejściowych.

Wyjątki

source to null.

Przykłady

W poniższym przykładzie kodu pokazano, jak wymusić ToList natychmiastową ocenę zapytania i zwrócić element List<T> zawierający wyniki zapytania.

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

Uwagi

Metoda ToList<TSource>(IEnumerable<TSource>) wymusza natychmiastową ocenę zapytania i zwraca element List<T> zawierający wyniki zapytania. Tę metodę można dołączyć do zapytania w celu uzyskania buforowanej kopii wyników zapytania.

ToArray ma podobne zachowanie, ale zwraca tablicę List<T>zamiast .

Dotyczy