Enumerable.ToList(TSource) 方法

来源:互联网 发布:无线打印服务器 mac 编辑:程序博客网 时间:2024/05/22 06:27
Enumerable.ToList(TSource) 方法

 

 

IEnumerable(TSource) 创建一个 List(TSource)

命名空间:   System.Linq
程序集:  System.Core(在 System.Core.dll 中)

异常条件ArgumentNullException

sourcenullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing

ToList(TSource)(IEnumerable(TSource) 方法强制进行直接查询计算,并返回一个包含查询值的 (T) 。可将此方法追加到您的查询,以获得查询结果的缓存副本。

ToArray(TSource)具有类似行为,但它返回一个数组,而非一个 List(T)

下面的代码示例演示如何使用 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.StringBuilderFor Each length As Integer In lengths    output.AppendLine(length)NextMsgBox(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 中的元素的类型。

参数

source
类型:System.Collections.Generic..::.IEnumerable (TSource)

要从其创建 List(T) IEnumerable(T)

返回值

类型:System.Collections.Generic..::.List (TSource)

一个包含输入序列中元素的 List(T)

使用说明

在 Visual Basic 和 C# 中,可以在 IEnumerable(TSource)类型的任何对象上将此方法作为实例方法来调用。当使用实例方法语法调用此方法时,请省略第一个参数。有关更多信息,请参见 扩展方法 (Visual Basic) 扩展方法(C# 编程指南)
原创粉丝点击