我有一个GridView和一行被删除,我触发GridView1_RowDeleting子,但我收到一个错误"LINQ到实体不识别方法'System.Web.UI.WebControls.TableCell get_Item(Int32)'方法,这个方法不能被翻译成存储表达式。"代码:
Private Sub GridView1_RowDeleting(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
' The deletion of the individual row is automatically handled by the GridView.
Dim dbDelete As New pbu_housingEntities
' Remove individual from the bed.
Dim remove_bed = From p In dbDelete.Beds _
Where p.occupant = GridView1.Rows(e.RowIndex).Cells(3).Text _
Where p.room = GridView1.Rows(e.RowIndex).Cells(6).Text _
Where p.building = GridView1.Rows(e.RowIndex).Cells(5).Text _
Order By p.id Descending _
Select p
remove_bed.First.occupant = ""
dbDelete.SaveChanges()
' Increase number of open spaces in room.
Dim update_occupancy = From p In dbDelete.Rooms _
Where p.room1 = GridView1.Rows(e.RowIndex).Cells(6).Text
Where p.building = GridView1.Rows(e.RowIndex).Cells(5).Text _
Select p
update_occupancy.First.current_occupancy = update_occupancy.First.current_occupancy - 1
dbDelete.SaveChanges()
End Sub
出错的具体行是:
remove_bed.First.occupant = ""
这是因为Linq查询被转换为SQL,而没有办法将GridView1.Rows(e.RowIndex).Cells(3).Text
转换为SQL。您需要从GridView中提取值,然后在查询
Dim occupant As String = GridView1.Rows(e.RowIndex).Cells(3).Text
Dim room As String = GridView1.Rows(e.RowIndex).Cells(6).Text
Dim building As String = GridView1.Rows(e.RowIndex).Cells(5).Text
Dim remove_bed = From p In dbDelete.Beds _
Where p.occupant = occupant _
Where p.room = room _
Where p.building = building _
Order By p.id Descending _
Select p
您必须在执行查询之前将这些值放入变量中,否则实体提供程序将尝试将整个对象拉入查询并在试图将其转换为SQL语句时访问其属性-这将失败,因为没有等同的SQL。