How to convert an IQueryable<T> to a List<T>?

Prabu picture Prabu · Aug 10, 2009 · Viewed 31k times · Source

Just learning LINQ and i've come to a newbie roadblock in my test project. Can you explain what i'm doing wrong?

public List<ToDoListInfo> retrieveLists(int UserID)
{
//Integrate userid specification later - need to add listUser table first
IQueryable<ToDoListInfo> lists = 
    from l in db.ToDoLists
    select new ToDoListInfo { 
        ListID = l.ListID, 
        ListName = l.ListName, 
        Order = l.Order, 
        Completed = l.Completed 
    };

    return lists.ToList<ToDoListInfo>;
}     

I'm getting an error saying the following:

Cannont convert method group 'ToList' to non-delegate type 'System.Collections.Generic.List' Do you intend to invoke the method?

Answer

Thomas Danecker picture Thomas Danecker · Aug 10, 2009

You just need parantheses:

lists.ToList<ToDoListInfo>();

Also, you do not have to declare the type parameter, i.e. you could use the following and let the type-system infer the type parameter:

lists.ToList();