0

我正在尝试从 SQL Server 数据库中获取数据。

我的数据库中有一个名为的表Standard。它具有三列StandardIDStandardNameDescription

我有一个组合框,我在其中填写StandardName

这是代码:

Using db As New SchoolDBEntities
    ComboSelectStandardToEdit.DataSource = db.Standards.ToList()
    ComboSelectStandardToEdit.ValueMember = "StandardID"
    ComboSelectStandardToEdit.DisplayMember = "StandardName"
End Using

现在我有 2 个名为txtStandardName和的文本框txtDescription

我想根据StandardName从组合框中选择的内容填充这两个文本框的值。

这是我尝试过的代码:

Using db As New SchoolDBEntities
            Dim standard = From s In db.Standards
                            Where s.StandardId = CInt(ComboSelectStandardToEdit.SelectedValue)
                            Select s

            txtStandardName.Text = CType(standard, Standard).StandardName
        End Using

但不幸的是我得到了错误:

无法转换类型为“System.Data.Entity.Infrastructure.DbQuery”的对象`1[EF_WinForms_VB.Standard]' to type 'EF_WinForms_VB.Standard'.

4

2 回答 2

2

尝试使用

Dim standard = (From s In db.Standards
    Where s.StandardId = CInt(ComboSelectStandardToEdit.SelectedValue)
    Select s)
    .FirstOrDefault

txtStandardName.Text = standard.StandardName

您的 Linq 查询当前正在返回一个可能包含多个条目的投影。通过显式请求投影的第一个对象,您无需在访问它的值之前转换您的标准。

于 2013-07-29T13:02:32.717 回答
1

尝试使用

暗淡标准 = (From s In db.Standards.AsEnumerable Where s.StandardId = Convert.ToInt32(ComboSelectStandardToEdit.SelectedValue) Select s) .FirstOrDefault

txtStandardName.Text = 标准.StandardName

希望能帮助到你。

于 2013-08-02T06:19:36.267 回答