C# 使用SilverLight Linq到SQL从2个SQL表中删除
我在SQL中得到了两个表,它们都有一个名为Selection\u ID的字段。我想使用Linq to SQL删除所有带有C# 使用SilverLight Linq到SQL从2个SQL表中删除,c#,silverlight,linq-to-sql,C#,Silverlight,Linq To Sql,我在SQL中得到了两个表,它们都有一个名为Selection\u ID的字段。我想使用Linq to SQL删除所有带有Selection\u ID=inpSelectionID的行 我的桌子: 我的C#函数: void buttonDeleteSelectionList_Click(object sender, RoutedEventArgs e, Canvas canvasAdvancedSearchFieldResults, int inpSelectionID) { Posi
Selection\u ID=inpSelectionID
的行
我的桌子:
我的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);
}
[OperationContract]
void DeleteSelection(int inpSelectionID)
{
PositionDataClassesDataContext context = new PositionDataClassesDataContext();
context.Lloyds_Selection.DeleteOnSubmit(inpSelectionID);
context.SubmitChanges();
context.Lloyds_Selection_Vessels.DeleteOnSubmit(inpSelectionID);
context.SubmitChanges();
}
deleteosubmit
需要实体对象作为参数,因此,如果不先从数据库中选择该项,则无法删除该项。还有deleteAllonsSubmit
方法,但它也需要实体的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 );
谢谢正是我想要的;-)