c# - Deleting items more than 1 day old from sql database -
i trying remove items created more 1 day old, items still remain. changes committed database since using removeitem
method in other locations well.
using (var db = new commerceentities()) { datetime checkexpirydate = datetime.utcnow.adddays(-1); var itemstodelete = c in db.shoppingcarts c.datecreated < checkexpirydate select new { c.cartid, c.productid, c.color, c.size, }; foreach (var item in itemstodelete) { try { //remove item code.... //removeitem(string cartid, int productid, string color, string size) } public void removeitem(string cartid, int productid, string color, string size) { using (var db = new commerceentities()) { .... .. //db.deleteobject(myitem); .... .. //db.savechanges(); ... .... .. } }
imo, preferred way tsql, not orm
declare @when datetime = getutcdate() - 1 delete shoppingcarts datecreated < @when
which trivial execute via either ado.net or helper utility.
that avoids dragging unknown amount of data on wire, , avoids lengthy transaction while perform multiple discreet operations.
Comments
Post a Comment