Not able to access .ToList() method on Table

franklins picture franklins · Oct 7, 2011 · Viewed 9.9k times · Source

I am trying to use SilverlightPhoneDatabase to use a sqlite db for my Windows Phone 7 application.

The dll has a class Database

    public class Database
    {
    .
    .
    .
    public Table<T> Table<T>();
    .
    .
    }

Definition of class Table is as follows:

public class Table<T> : ObservableCollection<T>, ITable

Then in my App.xaml I have a method

 public static Database MyDB
 {
get
{
// Delay creation of the view model until necessary
if (database == null)
OpenDatabase<MyObj>("MyObj");
return database;
}
}

Now from my xaml.cs file I am able to access the .ToList() method on the Table. ie If I say

App.MyDB.Table<MyObj>().ToList();

from my main.xaml.cs, I can access the ToList() method (Table class inherited from ObservableCollection..)

But I created a new class in a new cs in the same namespace.. and if I try to access the ToList() method on the Table class, I am getting compiler error that Table<> does not contain definition for 'ToList'

I am just curious why is it not working? It may be something silly I am missing.

Answer

Julien Roncaglia picture Julien Roncaglia · Oct 7, 2011

ToList is an extension method on IEnumerable<T> : System.Linq.Enumerable.ToList so you need to add an using for the System.Linq namespace to be able to use it.

The compiler is basically rewriting

App.MyDB.Table<MyObj>().ToList();

as

System.Linq.Enumerable.ToList(App.MyDB.Table<MyObj>());