Given this expression:
var q = (from c in db.ContattiTitoliStudio
where ...
orderby c.Data descending
select new
{
c.ID,
Date = c.Data.ToString("MM dd, YYYY"),
});
c.Data is nullable
, but also with a non-nullable var is the same: " ...No overload for method 'ToString' takes 1 arguments...
"
I tried nullable
, not nullable, with (c.Data.=null)...
, with String.Format
, with DateTime.Parse
, etc... But I can't format c.Date
the way I want.
In SQL Server I use datetime
as the type.
If I use normal Date = c.Date
this will be displayed as "01/01/2011 0.0.00".
Make use of Nullable(ie?? operator) Operation which is exists in C#
var q = (from c in db.ContattiTitoliStudio
where ...
orderby c.Data descending
select new
{
c.ID,
Date = (c.Data ?? DateTime.MinValue).ToString("MM dd, YYYY"),
});
Note: DateTime.MinValue is used when the value of c.Data is null which used to avoid null
Have you tried this?
Date = c.Data.HasValue ? c.Data.Value.ToString("MM dd, YYYY") : string.Empty
Ok,finally this is the solution:
var test= from e in db.Employees.ToList() select new {Time= e.Date.ToString()};
from msdn forums:
You didn't tell us, but I'm guessing that ContattiTitoliStudio.Data
is Nullable<DateTime>
. In this case, you should say
Date = c.Data.HasValue ? "null" : c.Data.Value.ToString("MM dd, YYYY")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.