LINQ to Entities无法识别方法 - 简单删除语句

时间:2021-09-12 02:15:57

I have a GridView and on a row being deleted I trigger the GridView1_RowDeleting sub, but I receive an error "LINQ to Entities does not recognize the method 'System.Web.UI.WebControls.TableCell get_Item(Int32)' method, and this method cannot be translated into a store expression." Code is:

我有一个GridView并在一行被删除我触发GridView1_RowDeleting子,但我收到一个错误“LINQ to Entities无法识别方法'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

The specific line erroring out is:

错误的具体行是:

remove_bed.First.occupant = ""

2 个解决方案

#1


4  

That's because the Linq query is translated to SQL, and there is no way to translate GridView1.Rows(e.RowIndex).Cells(3).Text to SQL. You need to extract the values from the GridView, then use them in the query

那是因为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

#2


1  

You have to put those values in variables before executing the query, otherwise the Entity Provider will try to pull the whole object into the query and access it's properties when it is trying to translate it into a SQL statement - which fails since there is no SQL equivalent.

您必须在执行查询之前将这些值放在变量中,否则实体提供程序将尝试将整个对象拉入查询并在尝试将其转换为SQL语句时访问它的属性 - 由于没有SQL,因此失败当量。

#1


4  

That's because the Linq query is translated to SQL, and there is no way to translate GridView1.Rows(e.RowIndex).Cells(3).Text to SQL. You need to extract the values from the GridView, then use them in the query

那是因为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

#2


1  

You have to put those values in variables before executing the query, otherwise the Entity Provider will try to pull the whole object into the query and access it's properties when it is trying to translate it into a SQL statement - which fails since there is no SQL equivalent.

您必须在执行查询之前将这些值放在变量中,否则实体提供程序将尝试将整个对象拉入查询并在尝试将其转换为SQL语句时访问它的属性 - 由于没有SQL,因此失败当量。