asp.net mvc - C# string contains daterange which should splitted into 2 dates -
i trying solve problem. have string filled date range in following format "dd.mm.yyyy - dd.mm.yyyy".
var datest = shippeddaterange.split(new char[] { '-' }, stringsplitoptions.removeemptyentries); var startdate = datetime.parse(datest[0]); var enddate = datetime.parse(datest[1]);
now want split 2 variables, can use lambda query works fine. 2 new variables should named startdate
, enddate
.
data.container = db.container .where(a => a.shippeddate >= startdate && a.shippeddate <= enddate) .tolist();
if compile app, gives me error, array value out of index range
you need defensive coding. don't assume goldilocks situation in rude awakening. if input bad, there stands chance wont end 2 items.
public partialviewresult searchdata(shippeddaterange) { data = new contentviewmodel(); using (var db = new plsdb()) { var datest = shippeddaterange.split(new char[] { '-' }, stringsplitoptions.removeemptyentries); if(datest.length == 2) { var format = "dd.mm.yyyy"; var provider = cultureinfo.invariantculture; var startdate = datetime.parseexact(datest[0].trim(), format, provider); var enddate = datetime.parseexact(datest[1].trim(), format, provider); data.container = db.container .where(a => a.shippeddate >= startdate && a.shippeddate <= enddate) .tolist(); } } return partialview(data); }
Comments
Post a Comment