ASP.NET LINQ 删除行

发布于 2024-08-31 00:31:22 字数 459 浏览 5 评论 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 个表中删除记录,然后再删除下一个表。

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

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

I'm getting an error that says:
Unable to cast object of type 'System.Data.Linq.DataQuery`1[WhiteWaterPhotos.Photo]' to type 'WhiteWaterPhotos.Photo'.

I have two separate db.SubmitChanges() because when the button is pressed, I have it delete the records from 1 table, and then the next.

I'm lost, can someone help me out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神爱温柔 2024-09-07 00:31:22

试试这个:

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

try this:

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

Or for a list of elements:

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
抚你发端 2024-09-07 00:31:22

尝试一下,

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# 中,在该查询的末尾会有一个“选择船”并使用
全部删除(...)

try

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

I don't know vb.net that much but in c# is would have a "select boat" at the end of that query and use
RemoveAll(...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文