c# - How do I remove elements from a table? -
allow me explain situation.
i have table in database represented in model by
public table<linkdate> dates; [table(name = "dates")] public class linkdate { public linkdate() { } [column(isprimarykey = true)] public string linkguid { get; set; } [column] public datetime dtime { get; set; } }
which records day/time ids generated. these ids associated 1 or more files in table represented
[table( name = "links")] public class assetlink { public assetlink() { } [column(isprimarykey = true, isdbgenerated = true)] public int linkid { get; set; } [column] public string linkguid { get; set; } [column] public int fileid { get; set; } }
i'm adding functionality in web application user can delete links on number of days old. means if such links exist, deleting rows both links
, dates
.
the action i've started create is
[httppost] public actionresult flushlinks (string numdaysold) { // deletes database references links submitted on numdaysold days ago datetime currentdatetime = datetime.now; foreach (linkdate thislinkdate in pd.dates) { timespan thistimespan = new timespan(convert.toint16(numdaysold), 0, 0, 0); if ((currentdatetime - thislinkdate.dtime) > thistimespan) { // ... } } try { pd.submitchanges(); } catch (exception ex) { return content(ex.message, "text/html"); } // if here, went return content("successfully removed old links!", "text/html"); }
and need filling out // ...
part because don't know how to, , can't see documentation way to, delete rows. i'm trying equivalent of
pd.links.remove(x => x.linkguid == thislinkdate.linkguid); // need if possible thislinkdate.remove() // , need if possible
what need do?
also, please let me know bugs see in procedure.
edit: think figured out myself:
foreach (assetlink thislink in pd.links) if (thislink.linkguid == thislinkdate.linkguid) pd.links.deleteonsubmit(thislink); pd.dates.deleteonsubmit(thislinkdate);
if that's wrong, let me know
try replacing foreach loop this.
datetime currentdatetime = datetime.now; timespan thistimespan = new timespan(convert.toint16(numdaysold), 0, 0, 0); var delete_list = pd.dates.where(x => (currentdatetime - x.dtime) > thistimespan) pd.dates.removerange(delete_list);
Comments
Post a Comment