Best way to delete multiple records in a LINQ query?

后端 未结 10 767
暗喜
暗喜 2021-01-07 17:22

What is the best way to remove multiple records in one go with LINQ?

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-07 18:04

    I agree with Khurram, it's much more efficient to do this with a simple stored procedure with LINQ (provided you have sufficient permissions in SQL to do this). I'll augment this with an example.

    The stored procedure:

    CREATE PROCEDURE [dbo].[RemovePermissionsFromRole] 
    (
        @ROLE_ID int
    )
    AS
    BEGIN
        SET NOCOUNT ON;
    
        DELETE FROM [RolePermissions] WHERE [RoleID] = @ROLE_ID;
    END
    

    Drag the stored procedure from the database explorer onto the methods pane in your DBML file. Save the file. And in code:

    if (Request.QueryString["RoleID"] != null) {
        int roleID = Convert.ToInt32(Request.QueryString["RoleID"]);
    
        SETSDataContext context = new SETSDataContext();
        context.RemovePermissionsFromRole(roleID);
    }
    

提交回复
热议问题