Skip to main content
.NET Framework Class Library
EnumerableToListTSource Method

Creates a ListT from an IEnumerableT.

Namespace:   System.Linq
Assembly:  System.Core (in System.Core.dll)
Syntax
<[%$TOPIC/bb342261_en-us_VS_110_3_0_0_0_0%]> _
Public Shared Function ToList(Of TSource) ( _
	source As [%$TOPIC/bb342261_en-us_VS_110_3_0_0_0_1%](Of TSource) _
) As [%$TOPIC/bb342261_en-us_VS_110_3_0_0_0_2%](Of TSource)
public static [%$TOPIC/bb342261_en-us_VS_110_3_0_1_0_0%]<TSource> ToList<TSource>(
	this [%$TOPIC/bb342261_en-us_VS_110_3_0_1_0_1%]<TSource> source
)
[[%$TOPIC/bb342261_en-us_VS_110_3_0_2_0_0%]]
public:
generic<typename TSource>
static [%$TOPIC/bb342261_en-us_VS_110_3_0_2_0_1%]<TSource>^ ToList(
	[%$TOPIC/bb342261_en-us_VS_110_3_0_2_0_2%]<TSource>^ source
)
static member ToList : 
        source:[%$TOPIC/bb342261_en-us_VS_110_3_0_3_0_0%]<'TSource> -> [%$TOPIC/bb342261_en-us_VS_110_3_0_3_0_1%]<'TSource> 

Type Parameters

TSource

The type of the elements of source.

Parameters

source
Type: System.Collections.GenericIEnumerableTSource

The IEnumerableT to create a ListT from.

Return Value

Type: System.Collections.GenericListTSource
A ListT that contains elements from the input sequence.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerableTSource. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Exceptions
ExceptionCondition
ArgumentNullException

source is .

Remarks

The ToListTSource(IEnumerableTSource) method forces immediate query evaluation and returns a ListT that contains the query results. You can append this method to your query in order to obtain a cached copy of the query results.

ToArrayTSource has similar behavior but returns an array instead of a ListT.

Examples

The following code example demonstrates how to use ToListTSource to force immediate query evaluation and return a ListT that contains the query results.

        ' 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
            */
Version Information

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1
Platforms

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.