我想从SQL Server数据库中获取数据。在实体框架中使用linq时获取类型转换错误
我在我的数据库中有一个名为Standard
的表。它有三列StandardID
,StandardName
和Description
。
我有一个组合框在我填补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'.
T在运行时出现此错误:无法创建类型为'System.Object'的常量值。只有原始类型或枚举类型在此上下文中受支持。 – Khushi
似乎这可能与你的where子句有关,在某一点你正在使用'StandardID',而在另一个你正在使用的'StandardId'不是对象 – Claies
@ user2524507的属性:你probaby必须提取'在Dim标准行之前的CInt转换:Dim standardId = CInt(ComboSelectStandardToEdit.SelectedValue),然后是Where子句:其中s.StandardId = standardId – Slauma