【问题标题】:Get database row from selected item in Combobox从组合框中的选定项目获取数据库行
【发布时间】:2023-04-08 07:19:01
【问题描述】:

我有一个从我的数据库中获取数据的组合框。

var people = (from x in db.Person select new { Value = x.Id, Names = x.Namn + " " + x.EfterNamn }).ToList();
cbpeople.DataSource = people;
cbpeople.DisplayMember = "Names";
cbpeople.ValueMember = "Value";
cbpeople.SelectedIndex = -1;

我有 SelectedIndex 函数

int id = cbpeople.SelectedIndex + 1;
string namn = (from x in db.Person where x.Id == id select x.Namn).ToString();
lblNamn.Text = namn;

如您所见,我试图让它从数据库的同一行中选择信息并将它们放在标签中。 (“cbpeople.SelectedIndex + 1;”是因为我没有其他方法可以从 SelectedValue 中获取 ID)。

但它打印出来的只是这个很长的东西,而不是名称(在标签上)

"SELECT \r\n    [Extent1].[Namn] AS [Namn]\r\n    FROM [dbo].[Person] AS [Extent1]\r\n    WHERE [Extent1].[Id] = @p__linq__0"

我做错了什么?

【问题讨论】:

    标签: c# winforms linq combobox


    【解决方案1】:

    您在 IQueryable 对象上调用 ToString()。当然,它会返回它的 SQL 表示。要执行查询,您可以这样做:

    string namn = (from x in db.Person where x.Id == id select x.Namn).Single();
    

    【讨论】:

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