Hooked on LINQ

Hooked on LINQ - Developers' Wiki
for .NET Language Integrated Query

Quick Search

Advanced Search »

ToList Operator

Modified: 2008/02/02 17:43 by arribajuan - Categorized as: LINQ to Objects
{TOC}
Namespace:System.Linq
Assembly:System.Core.dll
Extends:IEnumerable<T>

Back to Standard Query Operator Index


Edit

Introduction

The ToList operator creates a List from a sequence.

Edit

Method Signatures

public static List<TSource> ToList<TSource>(
    this IEnumerable<TSource> source)



Edit

Exceptions

Throws an ArgumentNullException if source is null.


Edit

Pseudo-code

If source is null, throw an ArgumentNullException.
Create a new List<TSource>(source).
Return the new list.


Edit

Loop count

1. The constructor of List<TSource> enumerates the sequence and adds it to the list.

Edit

Sample code

// Open database context
MyDataContext db = new MyDataContext();

// Perform the query
var result = from p in db.Players select p;

// Get the List
List<Player> myList = result.ToList();

// Iterate
foreach (Player currentPlayer in myList)
{
string value = currentPlayer.Name;
}

If you would like to comment on this page, click on the Discuss button located on the top-right of each page. Feel free to edit any mistakes or ommissions you find. If you have an objection or find in-appropriate content then contact the administrator. This website is not affiliated with Microsoft®, all content and opinions are those of the specific author and some advice, solutions and article may contain un-intentional errors - please use care. Powered by ScrewTurn Wiki version 2.0.33. Some of the icons created by FamFamFam.