I am trying to accept a DateTime value, and if it is not null, return a short time string. My query looks like this: (TimeIn is not NULLABLE, while TimeOut is NULLABLE)
var times = from t in db.TimePostings where t.MemberID == member.MemberID select new { Date = t.TimeIn.ToShortDateString(), TimeIn = t.TimeIn.ToShortTimeString(), TimeOut = t.TimeOut.HasValue ? t.TimeOut.Value.ToShortTimeString() : "-------" }; gvTimePostings.DataSource = times; gvTimePostings.DataBind();
but this fails when I try to bind data with an error:
Failed to translate the expression "Table (TimePosting). Where (t => (t.MemberID == Invoke (value (System.Func 1[System.String])))).Select(t => new <>f__AnonymousType8 4 ( Date = t.TimeIn.ToShortDateString (), TimeIn = t.TimeIn.ToShortTimeString (), TimeOut = IIF (t.TimeOut.HasValue, (t.TimeOut ?? Call (value (System.Func`1 [System.DateTime] ))). ToShortTimeString (), "-------"), Hours = "")) "in SQL and cannot be considered as a local expression.
I also get a similar error if I try to use:
TimeOut = t.TimeOut.HasValue ? Convert.ToDateTime(t.TimeOut).ToShortTimeString() : "-------"
however, if I changed the TimeOut property to:
TimeOut = t.TimeOut.HasValue ? t.TimeOut.ToString() : "-------",
It works fine, but does not format the time as I want it (shortTimeString).
what's up with that?
matthew_360
source share