Enumerable.ToList<TSource>(IEnumerable<TSource>) Método

Definição

Cria um List<T> de um 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)

Parâmetros de tipo

TSource

O tipo dos elementos de source.

Parâmetros

source
IEnumerable<TSource>

O IEnumerable<T> por meio do qual um List<T> será criado.

Retornos

List<TSource>

Um List<T> que contém elementos da sequência de entrada.

Exceções

source é null.

Exemplos

O exemplo de código a seguir demonstra como usar ToList para forçar a avaliação de consulta imediata e retornar um List<T> que contém os resultados da consulta.

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

Comentários

O ToList<TSource>(IEnumerable<TSource>) método força a avaliação de consulta imediata e retorna um List<T> que contém os resultados da consulta. Você pode acrescentar esse método à sua consulta para obter uma cópia armazenada em cache dos resultados da consulta.

ToArray tem um comportamento semelhante, mas retorna uma matriz em vez de um List<T>.

Aplica-se a