Prabu
Prabu

Reputation: 4197

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

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?

Upvotes: 13

Views: 33161

Answers (2)

Nathan W
Nathan W

Reputation: 55502

You are just missing the closing brackets on ToList, should be:

 ToList();

or

ToList<ToDoListInfo>();

Upvotes: 5

Thomas Danecker
Thomas Danecker

Reputation: 4685

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();

Upvotes: 29

Related Questions