Enumerable.ToList(TSource) 方法

本文介绍了Enumerable.ToList(TSource)方法,该方法用于将IEnumerable(TSource)转换为List(TSource),并提供了VisualBasic和C#示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.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

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# 编程指南)
有关调用实时(JIT)调试而不是此对话框的详细信息, 请参见此消息的结尾。 ************** 异常文本 ************** Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid column name 'Jjcd'. Invalid column name 'SendId'. at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at Microsoft.Data.SqlClient.SqlDataReader.TryConsumeMetaData() at Microsoft.Data.SqlClient.SqlDataReader.get_MetaData() at Microsoft.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted) at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean isAsync, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest) at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String method) at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at Microsoft.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior) at Microsoft.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject) at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.InitializeReader(Enumerator enumerator) at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.<>c.<MoveNext>b__21_0(DbContext _, Enumerator enumerator) at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded) at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at 应急广播数据推送.ResultQuery.select_bt_Click(Object sender, EventArgs e) in G:\2025项目\应急广播数据推送\应急广播数据推送\ResultQuery.cs:line 114 at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(HWND hWnd, MessageId msg, WPARAM wparam, LPARAM lparam) ClientConnectionId:6325314d-04d3-4433-a295-218cf8df4b83 Error Number:207,State:1,Class:16
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值