0

SQLで2つのテーブルを取得しましたが、どちらにもSelection_IDというフィールドがあります。Selection_ID = inpSelectionIDLinq-to-SQLを使用してすべての行を削除したい。

私のテーブル: ここに画像の説明を入力してください

私の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();
}
4

1 に答える 1

1

DeleteOnSubmitパラメータとしてエンティティオブジェクトが必要なため、最初にデータベースからアイテムを選択せず​​にアイテムを削除することはできません。方法もありますが、実体も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 );
于 2013-03-14T12:08:11.757 に答える