【问题标题】:Get Data From Sqlite Database with Xamarin.Forms使用 Xamarin.Forms 从 Sqlite 数据库中获取数据
【发布时间】:2018-06-24 05:54:03
【问题描述】:

当我使用 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();
    }


}

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

【问题讨论】:

    标签: sqlite xamarin xamarin.forms xamarin.android android-sqlite


    【解决方案1】:

    这部分查询和 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;
    

    【讨论】:

    • 非常感谢。我现在试试。但它给出了另一个错误:“条目”不包含“选择”的定义,并且找不到接受“条目”类型的第一个参数的扩展方法“选择”(您是否缺少 using 指令或程序集引用? )
    • @TanVuBoyGib 我更新了答案以删除最终的Select.FirstOrDefault()?.defn;
    • 再次感谢您。你帮了我很多。我现在就试试。
    • 我试过了。这次它在模拟器屏幕上什么也没显示。我认为: dbConnection.Table().Where(x => x.word == w) 像你说的那样返回零
    • 我对这个语句的想法是这样的:select defn from Entries where word = w
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    相关资源
    最近更新 更多