c# - Convert datetime2 to string("dd/MM/yyyy") in LINQ -
i have datetime2 column in database.i'm trying fetch database , want display in view string.
i tried: dt.tostring("dd/mm/yyyy");
but gave error.
an exception of type 'system.notsupportedexception' occurred in entityframework.sqlserver.dll not handled in user code
additional information: linq entities not recognize method 'system.string tostring(system.string)' method, , method cannot translated store expression.
i tried string.format("{0:y yy yyy yyyy}", dt);
also, throwed error.
i think realize problem, after posted screenshot. functions aren't allowed run inside linq, linq needs translate them sql.
since can't copy paste code modifications (you need perform date formatting outside linq statement, whether using foreach loop, or other method). rough idea:
allclient = (from .... select new allclient() { ... purchasedt = "", purchasedate = k.purchasedate, //the formatting done later } foreach (allclient c in allclient) c.purchasedt = string.format("{0:y yy yyy yyyy}", c.purchasedate); //or whatever other formatting want
where purchasedate
of type datetime , purchasedt
of type string.
first save value coming db datetime variable , later loop through list using foreach loop,convert datetime variable string , assign string variable.
updated
the way involve adding 1 more variable allclient class, if can. datetime temporarily store date, 1 string (as shown in code snippet above), updated formatted date after linq statement.
or
keep single datetime purchasedt
, , instead of formatting in linq or afterwards, format when displaying it. example, if you're displaying records on table of kind, while displaying date column, formatting on purchasedt
there.
Comments
Post a Comment