Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 390
Prabu Avatar asked Aug 10 '09 06:08

Prabu


People also ask

Is IQueryable faster than list?

GetList(context) might return an object backed by a custom LINQ provider (like an Entity Framework collection), then you probably want to leave the data cast as an IQueryable: even though your benchmark shows it being 20 times faster to use a list, that difference is so small that no user is ever going to be able to ...

What inherits from IQueryable?

The IQueryable interface inherits the IEnumerable interface so that if it represents a query, the results of that query can be enumerated. Enumeration causes the expression tree associated with an IQueryable object to be executed.

What is the difference between IEnumerable T and IQueryable T >? And how do you switch between them?

The main difference between IEnumerable and IQueryable in C# is that IQueryable queries out-of-memory data stores, while IEnumerable queries in-memory data. Moreover, IQueryable is part of . NET's System. LINQ namespace, while IEnumerable is in System.

What is IQueryable in C# with example?

IQueryable is suitable for querying data from out-memory (like remote database, service) collections. While querying data from a database, IQueryable executes a "select query" on server-side with all filters. IQueryable is beneficial for LINQ to SQL queries.


2 Answers

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

like image 94
Thomas Danecker Avatar answered Sep 28 '22 11:09

Thomas Danecker


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

 ToList();

or

ToList<ToDoListInfo>();
like image 36
Nathan W Avatar answered Sep 28 '22 12:09

Nathan W