gpt4 book ai didi

sqlite - 使用 Xamarin.Forms 从 Sqlite 数据库获取数据

转载 作者:行者123 更新时间:2023-12-02 09:31:39 25 4
gpt4 key购买 nike

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

这是我的 Entries 类:

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; }

这是DatabaseManager 类。 function 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 从枚举中获取第一条记录(如果不存在记录,则使用 FirstOrDefault 返回 null ):

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

关于sqlite - 使用 Xamarin.Forms 从 Sqlite 数据库获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51007308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com