EF:删除很多

时间:2013-08-21 08:09:05

标签: asp.net-mvc entity-framework asp.net-mvc-4 entity-framework-5

在MVC应用程序中处理的每日导入过程中,我需要在执行实际导入之前删除在固定timehorizo​​n中具有日期戳的所有实体。有没有办法带这个代码..

var deleteShows = db.Shows.Where(x => x.begin >= DateTime.Today.Date).ToList();
foreach (Show show in deleteShows)
    {
        db.Shows.Remove(show);
    }
db.SaveChanges();

..更短/更快的版本,如

db.Shows.Where(x => x.begin >= DateTime.Today.Date).ToList().ForEach(db.Shows.Remove(???));

1 个答案:

答案 0 :(得分:2)

确实如此:

db.Shows.Where(x => x.Date >= DateTime.Today.Date)
    .ToList().ForEach( s => db.Shows.Remove(s));