c# - Assigning blank value to a DateTime filed -
i have written linq xml query, create entities xml, have datetime field in xml can possibly blank, , need assign same blank field in entity, getting error "string not recognized datetime"
xdocument xdocument = xdocument.load(@"c:\sample.xml");
var _pndlist = plist in xdocument.descendants("header") select new pnd() { deliverydate = datetime.parseexact(convert(plist, "deliverydate"), "yyyymmdd", cultureinfo.invariantculture, datetimestyles.none), loadclosed = datetime.parseexact(convert(plist, "loadcloseddatetime"), "yyyymmddhhmmss", cultureinfo.invariantculture, datetimestyles.none), trailerid = convert(plist, "trailerid"), tripandrouteid = convert(plist, "triprouteid"), storecode = plist.ancestors("store").attributes().first().value, product = (from mlist in plist.ancestors("store").descendants("record") select new prenotifiedproduct() { depotcode = convert(plist, "depotcode"), mu = convert(mlist, "mu"), caseorunitquantity = system.convert.toint32("0" + convert(mlist, "noofcases")), outercaseheight = system.convert.toint32("0" + convert(mlist, "outercaseheight")), outercaselength = system.convert.toint32("0" + convert(mlist, "outercaselength")), outercasewidth = system.convert.toint32("0" + convert(mlist, "outercasewidth")), productcode = convert(mlist, "tpnb"), unitspercase = system.convert.toint32("0" + convert(mlist, "unitspercase")), usebydate = datetime.parseexact(convert(plist, "loadcloseddatetime"), "yyyymmddhhmmss", cultureinfo.invariantculture, datetimestyles.none) }).tolist() }; }
in entity prenotifiedproduct() usebydate of datetime
would possible set usebydate type of "datetime?" making nullable.
then may have write in logic test see if it's valid date. perhaps write function this.
static datetime? testdate(string date) { datetime result; if (datetime.tryparse("", out result)) { return result; } return null; }
Comments
Post a Comment