【问题标题】:SQLite query list objects contain only default property valuesSQLite 查询列表对象仅包含默认属性值
【发布时间】:2018-08-22 07:55:39
【问题描述】:

我正在尝试在 Xamarin.Forms Android 应用程序中使用 SQLite 数据库。查询表时,我在将列映射到 Game 类中的属性时遇到问题。所有属性都返回为null

我有一个类 Game 包含多个字段,NameType、...

public class Game {
    public string Name { get; set; }
    public int MinPlayers { get; set; }
    public int MaxPlayers { get; set; }
    public string Type { get; set; }
    public string IsParty { get; set; }
}

我的桌子是这样的:

Games

| Name  | MinPlayers | MaxPlayers | Type | IsParty |
----------------------------------------------------
| Catan | 2          | 5          | Base | false   |

在我的代码中,我目前正在尝试对此运行查询并得到意想不到的结果。

查询代码如下:

List<Game> games;

string query = "SELECT Name, MinPlayers, MaxPlayers, Type, IsParty FROM Games WHERE MinPlayers <= 4 AND MaxPlayers >= 4;";

using (var connection = new SQLiteConnection(_dbPath)) {
    games = connection.Query<Game>(query).ToList();
};

执行查询时,我返回了一个包含预期结果数量的Game 对象列表。但是在检查每个对象的内容时,所有属性都是null

来自 VS 调试器locals 窗口:

games
  |
  -- [0]
      |
      -- IsParty: null
      |
      -- MaxPlayers: 0
      |
      -- MinPlayers: 0
      |
      -- Name: null
      |
      -- Type: null

这对于列表中的所有条目都是相同的。我以前使用此代码并获得了预期的结果,但是我需要更改表格的布局,从那时起我一直遇到这个问题。我检查了属性的名称以确保它们与表列匹配,但这似乎不是问题。

如果我遗漏了任何重要信息,我很乐意提供。

【问题讨论】:

  • using之外games.Max(z =&gt; z.Name)的值是多少?
  • 是的,这是我现在修正的错字。 games.Max(z =&gt; z.Name)using 之外的值为null

标签: c# sqlite xamarin.forms


【解决方案1】:

您是否尝试过运行此命令以查看它是否返回某些内容?您可以使用 SQLiteStudio 或 SQLite Prime(Android) 等软件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多