我想從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