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>
要从其创建 List<T> 的 IEnumerable<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>.