LINQ to SQL批量删除更新

  • sql语句法删除

 

ItemDataContext db = new ItemDataContext(); 
db.ExecuteCommand( 
    "DELETE FROM Item WHERE [CreateTime] < {0}", 
    DateTime.UtcNow.AddMonths(-1));

//要想实现如下删除,需要http://weblogs.asp.net/jeffreyzhao/attachment/5919866.ashx这个扩展!

  • 单条件删除

 

ItemDataContext db = new ItemDataContext(); 
db.Items.Delete(item => item.CreateTime < DateTime.UtcNow.AddMonths(-1));

  • 多条件删除
ItemDataContext db = new ItemDataContext(); 
db.Items.Delete(item => 
    item.CreateTime < DateTime.UtcNow.AddMonths(-1) || 
    item.ViewCount < item.CommentCount && item.UserName != "jeffz"); 

 

  • 批量更新
ItemDataContext db = new ItemDataContext(); 

 

db.Items.Update( 
    item => new Item 
    { 
        Introduction = item.Title + "Hello World", 
        ViewCount = item.ViewCount + 1, 
    }, // SET [Introduction] = [Title] + 'Hello World', ... 
    item => item.CommentCount > 100 /* WHERE condition */);

摘自:Jeffrey Zhao‘s Blog

posted on 2008-09-10 21:33 sky老杨 阅读(...) 评论(...) 编辑 收藏

导航

公告