Enumerable.ToList(TSource) 方法
命名空间: System.Linq
程序集: System.Core(在 System.Core.dll 中)
异常
异常 | 条件 |
---|---|
ArgumentNullException | source 为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing)。 |
备注
示例
下面的代码示例演示如何使用 ToList(TSource) 强制进行直接查询计算并返回一个包含查询结果的 List(T) 。
Visual Basic
' 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 MsgBox(output.ToString()) ' This code produces the following output: ' ' 5 ' 12 ' 6 ' 5 ' 6 ' 9 ' 5 ' 10
C#
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 */
语法
Visual Basic(声明)
<ExtensionAttribute> _ Public Shared Function ToList(Of TSource) ( _ source As IEnumerable(Of TSource) _ ) As List(Of TSource)
Visual Basic (用法)
Dim source As IEnumerable(Of TSource) Dim returnValue As List(Of TSource) returnValue = source.ToList()
C#
public static List<TSource> ToList<TSource>( this IEnumerable<TSource> source )
Visual C++
[ExtensionAttribute] public: generic<typename TSource> static List<TSource>^ ToList( IEnumerable<TSource>^ source )
J#
J# 支持使用泛型 API,但是不支持新泛型 API 的声明。
JScript
JScript 不支持泛型类型或方法。
类型参数
-
TSource
-
source 中的元素的类型。