Hi Mark,
I haven't fully tested it yet, but the following should work:
Code:
using (PlanetWroxEntities myEntities = new PlanetWroxEntities())
{
DateTime oldDate = DateTime.Now.AddMonths(-3);
var allOldAlbums = (from a in myEntities.PhotoAlbums
where a.CreateDate < oldDate
select a).ToList();
foreach (var album in allOldAlbums)
{
foreach (var picture in album.Pictures.ToList())
{
File.Delete(Server.MapPath(picture.ImageUrl));
myEntities.Pictures.DeleteObject(picture);
}
myEntities.PhotoAlbums.DeleteObject(album);
}
myEntities.SaveChanges();
}
Most of it is hopefully straight forward. For an explanation of why you need the two calls to ToList, check out this SO post:
http://stackoverflow.com/questions/3...n-foreach-loop
Hope this helps,
Imar