使用 Xamarin.Forms 从 Sqlite Database 获取数据



当我使用 Xamarin 和 sqlite 创建一个应用程序时。我运行一个语句,但它在模拟器屏幕中向我显示以下句子:System.Linq.Enumerable + SelectEnumerableIterator'2[AppUI.Entries, System.String]

这是我的条目类:

public class Entries
{
public Entries()
{
}
public Entries(string word)
{
this.word = word;
}
public Entries(string word, string wordtype, string definition)
{
this.word = word;
this.type = wordtype;
this.defn = definition;
}   
public string word
{ get; set; }
public string type
{ get; set; }
public string sdex { get; set; }
public int wlen { get; set; }
public string defn
{ get; set; }

函数 GetDef(( 是我推荐的语句。

public class DatabaseManager
{
SQLiteConnection dbConnection;
public DatabaseManager()
{
dbConnection = DependencyService.Get<ISQLite>().GetConnection();
}

public string GetDef(string w)
{
return dbConnection.Table<Entries>().Where(x => x.word == w).Select(x => x.defn).ToString();
}

}

我不知道为什么会这样?请帮忙。非常感谢

查询和 Linq 的这一部分返回一个IEnumerable(即零个或多个Entries对象(。

dbConnection.Table<Entries>().Where(x => x.word == w)

Linq 投影的这一部分假设它只有一个Entries对象:

.Select(x => x.defn).ToString();

因此,请使用First从枚举中获取第一条记录(如果不存在记录,则返回带有FirstOrDefaultnull(:

return dbConnection.Table<Entries>()
.Where(x => x.word == w)
.FirstOrDefault()?
.defn;

相关内容

最新更新