2010-04-30 59 views
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。照片'。ASP.NET LINQ删除行

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

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

回答

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 
+0

难道那只会删除一行吗?我希望它删除包含该ID的所有行。我还得到'序列包含多个元素' – Landmine 2010-04-30 01:26:04

+0

看看新代码 – 2010-04-30 01:28:33

+0

第二个代码给出这个错误--------- 错误10类型'System.Collections.Generic.List(WhiteWaterPhotos。照片)'不能转换为'WhiteWaterPhotos.Photo' – Landmine 2010-04-30 01:29:19

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(...)