sqlite - 使用 Xamarin.Forms 从 Sqlite 数据库中获取数据
问题描述
当我使用 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; }
这是 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
获取第一条记录(或返回if no records exist ):null
FirstOrDefault
return dbConnection.Table<Entries>()
.Where(x => x.word == w)
.FirstOrDefault()?
.defn;
推荐阅读
- python - 将蜂窝塔的估计位置分配给最近的实际蜂窝塔
- php - Laravel 无法声明类,因为该名称已在使用中
- python - 如何通过人气图表平均不同长度的路径?
- wordpress - 如果主题内置了 SEO,我还能使用 Yoast SEO 插件吗?
- python - 赋值前引用的局部变量“布尔”
- javascript - 我的代码只有六行,但在第 7 行“意外结束输入”失败
- kubernetes - 使用 bazel 部署示例 Kubernetes 部署时出错
- xcode - 如何修复“xcrun simctl io booted recordVideo”的条纹和倾斜输出?
- r - 如果字符串向量等于多个字符串向量中的元素,则创建等于一的虚拟变量
- docker - 如何忽略要在 docker build 上下文中发送的文件夹