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

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -