命名空間: System.Linq
程序集: System.Core(在 System.Core.dll 中)
異常 | 條件 |
---|---|
ArgumentNullException |
source 爲 nullNothingnullptrnull 引用(在 Visual Basic 中爲 Nothing)。 |
下面的代碼示例演示如何使用 ToList(TSource) 強制進行直接查詢計算並返回一個包含查詢結果的 List(T) 。
' 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 */
<ExtensionAttribute> _ Public Shared Function ToList(Of TSource) ( _ source As IEnumerable(Of TSource) _ ) As List(Of TSource)
Dim source As IEnumerable(Of TSource) Dim returnValue As List(Of TSource) returnValue = source.ToList()
public static List<TSource> ToList<TSource>( this IEnumerable<TSource> source )
[ExtensionAttribute] public: generic<typename TSource> static List<TSource>^ ToList( IEnumerable<TSource>^ source )
J# 支持使用泛型 API,但是不支持新泛型 API 的聲明。
JScript 不支持泛型類型或方法。
類型參數
- TSource
-
source 中的元素的類型。