【问题标题】:Populate a combobox with table column使用表格列填充组合框
【发布时间】:2018-02-20 18:41:14
【问题描述】:

我必须用 SQlite 表中的一列填充 ComboBox

我使用它来选择 Column 但我认为查询应该是字符串列表,我如何将(对象)转换为字符串列表?

 public List<string> SystemsNameList()
 {
    using (SQLiteConnection conn = new SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), App.DB_PATH))
    {
        var SystemsName = conn.Query<FFSystems>("select Name from FFSystems");
        return SystemsName;
    }
}

这是保存在数据库表中的对象的定义

public class FFSystems
    {
        [PrimaryKey][AutoIncrement]
        public int ID { get; set; }
        public string Name { get; set; }
        public int IDCode { get; set; }
        public bool Active { get; set; }
        public string CreationDate { get; set; }
        public FFSystems() { }

        public FFSystems(string name, int idcode, bool active)
        {
            Name = name;
            IDCode = idcode;
            Active = active;
            CreationDate = DateTime.Now.ToString();
        }
    }

【问题讨论】:

  • SystemsNames.Cast&lt;string&gt;().ToList() 工作吗?
  • 不,我在使用时遇到了 InvalidCastExcepion
  • SystemsName 是什么类型?
  • SystemsName 是表中的一个对象,但我在查询中只选择了一列字符串。
  • 试试return SystemsName.Select(n =&gt; n.Name).ToList();

标签: c# sqlite uwp


【解决方案1】:

您需要访问 FFSystems 中存储名称的属性。 您可以使用 LINQ 轻松获取。

return SystemsName.Select(n =&gt; n.Name).ToList();

【讨论】:

    猜你喜欢
    • 2020-09-03
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多