0
Dim db As New SQLDataContext
Try
    Dim deleteBoatPics = (From boat In db.Photos
                          Where boat.boatid = id)
    db.Photos.DeleteOnSubmit(deleteBoatPics)
    db.SubmitChanges()
Catch ex As Exception
End Try

我收到一条错误消息:无法将“System.Data.Linq.DataQuery`1[WhiteWaterPhotos.Photo]”类型的对象转换为“WhiteWaterPhotos.Photo”。

我有两个单独的 db.SubmitChanges() 因为当按下按钮时,我让它从 1 个表中删除记录,然后是下一个。

我迷路了,有人可以帮我吗?

4

2 回答 2

1

试试这个:

Dim db As New SQLDataContext
Try
    Dim deleteBoatPics = (From boat In db.Photos
                          Where boat.boatid = id).take(1).singleordefault
    if not deleteBoatPics is Nothing Then
       db.Photos.DeleteOnSubmit(deleteBoatPics)
       db.SubmitChanges()
    End If
Catch ex As Exception
End Try

或者对于元素列表:

Dim db As New SQLDataContext
Try
    Dim deleteBoatPics = (From boat In db.Photos
                          Where boat.boatid = id).ToList()
    if not deleteBoatPics is Nothing Then
       db.Photos.DeleteAllOnSubmit(deleteBoatPics)
       db.SubmitChanges()
    End If
Catch ex As Exception
End Try
于 2010-04-30T01:24:02.767 回答
1

尝试

Dim db As New SQLDataContext
Try
    Dim deleteBoatPics = (From boat In db.Photos
                          Where boat.boatid = id
                          select boat)
    db.Photos.RemoveAll(deleteBoatPics)
    db.SubmitChanges()
Catch ex As Exception
End Try

我不太了解 vb.net 但在 c# 中会在该查询的末尾有一个“选择船”并使用 RemoveAll(...)

于 2010-04-30T01:40:41.340 回答