【问题标题】:Store a SQLite Table in an ObservableCollection?将 SQLite 表存储在 ObservableCollection 中?
【发布时间】:2018-07-08 16:50:21
【问题描述】:
public ObservableCollection<AppUser> AppUser { get; set; }

public AppUserDbConnect(string dbPath)
{
    _database = new SQLiteAsyncConnection(dbPath);
    _database.CreateTableAsync<AppUser>().Wait();
    this.AppUser = new ObservableCollection<AppUser>(_database.Table<AppUser>());
}

此代码创建一个数据库连接,然后基于AppUser 模型创建一个表。我正在尝试存储最后一行的结果,但遇到以下错误:

CS1503 参数 1:无法从 Dialler1.DatabaseConnection.AsyncTableQuery&lt;AppUser&gt;System.Collections.Generic.IEnumerable&lt;AppUser&gt;

【问题讨论】:

  • 感觉就像您希望在将数据添加到该表时得到通知。那不会发生
  • 您使用的是异步 API,因此您需要等待结果,并将其转换为 List
  • 是的,这就是我在@pm100之后的目标
  • 那我该怎么做呢?您是否需要查看我的更多代码才能做出更明智的决定@pm100
  • 你想做什么。您是否想知道何时有人添加数据(即其他应用程序这样做)或您的应用程序何时添加数据

标签: c# xamarin.forms sqlite.net


【解决方案1】:

db.Table&lt;T&gt;()调用ToList

 this.AppUser = new ObservableCollection<AppUser>(_database.Table<AppUser>().ToList());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 2011-09-25
    • 1970-01-01
    • 2018-11-27
    相关资源
    最近更新 更多