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?
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 ...
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.
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.
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.
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();
You are just missing the closing brackets on ToList, should be:
ToList();
or
ToList<ToDoListInfo>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With