13

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?

casperOne
  • 73,706
  • 19
  • 184
  • 253
Prabu
  • 4,097
  • 5
  • 45
  • 66

2 Answers2

29

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

Thomas Danecker
  • 4,635
  • 4
  • 32
  • 31
  • 1
    Declaring the type name parameter might actually be a *bad* thing here, as you have to match the type parameter of the `lists` `IEnumerable` implementation, which could cause a compile-time error; better to let [type-inference](http://geekswithblogs.net/sdorman/archive/2007/04/06/111034.aspx) do it's job – casperOne Nov 10 '11 at 14:24
  • What if I want to convert the iqueryble collection to a different type of list? For example lets assume I have two class A & B with almost similar signature. Now I retrieve data from A & want make a list of type B, how can I do that? – Badhon Jain Nov 19 '13 at 04:30
5

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

 ToList();

or

ToList<ToDoListInfo>();
Nathan W
  • 54,475
  • 27
  • 99
  • 146