2013-03-14 70 views
0

我在SQL中有两个表都有一个名为Selection_ID的字段。我想使用Linq-to-SQL删除所有包含Selection_ID = inpSelectionID的行。使用SilverLight从2个SQL表中删除Linq-to-SQL

我的表: enter image description here

我的C#功能:

void buttonDeleteSelectionList_Click(object sender, RoutedEventArgs e, Canvas canvasAdvancedSearchFieldResults, int inpSelectionID) 
{ 

    PositionServiceReference.PositionServiceClient service = new PositionServiceReference.PositionServiceClient(); 
    service.DeleteSelectionCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(service_DeleteSelectionCompleted); 
    service.DeleteSelectionAsync(inpSelectionID); 
} 

我的服务代码PositionService.svc.cs:

[OperationContract] 
void DeleteSelection(int inpSelectionID) 
{ 
    PositionDataClassesDataContext context = new PositionDataClassesDataContext(); 

    context.Lloyds_Selection.DeleteOnSubmit(inpSelectionID); 
    context.SubmitChanges(); 

    context.Lloyds_Selection_Vessels.DeleteOnSubmit(inpSelectionID); 
    context.SubmitChanges(); 
} 

回答

1

DeleteOnSubmit requi将实体对象留作参数,因此如果不先从数据库中选择它,则无法删除该项目。也有DeleteAllOnSubmit方法,但它也需要IEnumerable实体。您可以使用它像:

context.Lloyds_Selection.DeleteAllOnSubmit(context.Lloyds_Selection.Where(l => l.Selection_ID == inpSelectionID)); 

但是,您可以使用DataContext.ExecuteCommand执行原始的SQL对数据库:

string command = string.format("DELETE FROM Lloyds_Section WHERE Selection_ID = {0}", inpSelectionID"); 
context.ExecuteCommand(command); 
+0

谢谢!正是我在找的;-) – Solo 2013-03-14 13:14:31