无法访问表上的.ToList()方法

fra*_*ins 3 c# windows-phone-7 tolist

我正在尝试使用SilverlightPhoneDatabase为我的Windows Phone 7应用程序使用sqlite数据库.

该DLL有一个类数据库

    public class Database
    {
    .
    .
    .
    public Table<T> Table<T>();
    .
    .
    }
Run Code Online (Sandbox Code Playgroud)

类的定义表如下:

public class Table<T> : ObservableCollection<T>, ITable
Run Code Online (Sandbox Code Playgroud)

然后在我的App.xaml中有一个方法

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

现在从我的xaml.cs文件中,我可以访问表上的.ToList()方法.即如果我说

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

从我的main.xaml.cs,我可以访问ToList()方法(Table类继承自ObservableCollection ..)

但是我在同一名称空间中的新cs中创建了一个新类..如果我尝试访问Table类上的ToList()方法,我收到编译器错误,表<>不包含'ToList'的定义

我只是好奇为什么它不起作用?这可能是我想念的傻事.

Jul*_*lia 17

ToList是一个扩展方法IEnumerable<T>:System.Linq.Enumerable.ToList所以你需要为System.Linq命名空间添加一个using 才能使用它.

编译器基本上是重写的

App.MyDB.Table<MyObj>().ToList();
Run Code Online (Sandbox Code Playgroud)

System.Linq.Enumerable.ToList(App.MyDB.Table<MyObj>());
Run Code Online (Sandbox Code Playgroud)