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

Definizione

Crea un oggetto List<T> da un oggetto 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)

Parametri di tipo

TSource

Tipo degli elementi di source.

Parametri

source
IEnumerable<TSource>

Oggetto IEnumerable<T> da cui creare un oggetto List<T>.

Restituisce

List<TSource>

Oggetto List<T> che contiene gli elementi dalla sequenza di input.

Eccezioni

source è null.

Esempio

Nell'esempio di codice seguente viene illustrato come usare ToList per forzare la valutazione immediata delle query e restituire un List<T> oggetto contenente i risultati della query.

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

Commenti

Il ToList<TSource>(IEnumerable<TSource>) metodo forza la valutazione immediata della query e restituisce un List<T> oggetto contenente i risultati della query. È possibile aggiungere questo metodo alla query per ottenere una copia memorizzata nella cache dei risultati della query.

ToArray ha un comportamento simile, ma restituisce una matrice anziché un oggetto List<T>.

Si applica a