linq-ef中如何批量删除

1、首先在数据库中建立函数

CREATE   FUNCTION [dbo].[Split]  
(  
@c VARCHAR(MAX) ,  
@split VARCHAR(50)  
)  
RETURNS @t TABLE ( col VARCHAR(50) )  
AS  
BEGIN  
    WHILE ( CHARINDEX(@split, @c) <> 0 )  
        BEGIN  
            INSERT  @t( col )  
            VALUES  ( SUBSTRING(@c, 1, CHARINDEX(@split, @c) - 1) )  
            SET @c = STUFF(@c, 1, CHARINDEX(@split, @c), '')  
        END  
    INSERT  @t( col ) VALUES  ( @c )  
    RETURN  
END  

2、再建立存储过程

USE [iLearning]
GO

/****** Object:  StoredProcedure [dbo].[usp_delete_courses]    Script Date: 04/26/2013 13:52:56 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


create proc [dbo].[usp_delete_courses]
@ids varchar(max)
as  
update t_course_addition set isactive='N',updatetime=getdate() where id in (select * from Split(@ids,','));
update t_course_comment set isactive='N',updatetime=getdate() where courseid in (select * from Split(@ids,','));
update t_course_previewimage set isactive='N',updatetime=getdate() where courseid in (select * from Split(@ids,','));
update t_course set isactive='N',updatetime=getdate() where id in (select * from Split(@ids , ','));

GO

3、最后,在DAL中调用:

var objectParm = new[] { new ObjectParameter("ids", deleteIdStr) };
                    return db.ExecuteFunction("iLearningEntities.usp_delete_courses", objectParm);

 

posted @ 2013-04-26 13:54  瓜王  阅读(646)  评论(0编辑  收藏  举报