c# - How to get Date without Time from DateTime while join tables -


i joining 3 tables , date without time.

var query = anobjective in db.objective     join anobjectivetype in db.objectivetype                 on anobjective.idtype equals anobjectivetype.idobjectivetype     join statuscode in db.statuscode                 on anobjective.idstatuscode equals statuscode.idstatus     select new     {           idobjective = anobjective.idobjective,           objectivename = anobjective.objectivename,           datecreation = anobjective.datecreation, //get date without time           dateend = anobjective.dateend,  //get date without time      }; 

i know there method dbfunctions.truncatetime, returns time 0 values. want date without time.

i've seen this answer, cannot figure out how apply when joining, not grouping.

model class:

public partial class objective {     public int idobjective { get; set; }     public string objectivename { get; set; }     public nullable<system.datetime> datecreation { get; set; }     public nullable<system.datetime> dateend { get; set; }     } 

how date value without time properties such anobjective.datecreation , anobjective.dateend?

my desirable result date: 1.9.2016

edit:
if write following syntax:

datecreation =anobjective.datecreation.date, //or datecreation =null ? default(datetime?) :  anobjective.datecreation.date, 

then i've got such exception:

'system.nullable' not contain definition 'date' , no extension method 'date' accepting first argument of type 'system.nullable' found (are missing using directive or assembly reference?)


if write:

datecreation =anobjective.datecreation.value.toshortdatestring(), anobjective.dateend.value.toshortdatestring(), 

then i've got such exception:

linq entities not recognize method 'system.string toshortdatestring()' method, , method cannot translated store expression

if write:

datecreation =anobjective.datecreation.value.date, 

then i've got such exception:

the specified type member 'date' not supported in linq entities. initializers, entity members, , entity navigation properties supported.

first, recognize there no built-in type date without time in .net. at least not yet anyway. if want date-only, you'll need in string in particular format.

you're not going able underlying provider of linq query understand conversion of datetime string. so, you'll need query data in original form, , then convert string after query results materialized.

start original query, unmodified showed @ top of question. add following:

var results = query.asenumerable().select(x=> new {     idobjective = x.idobjective,     objectivename = x.objectivename,     datecreation = x.datecreation.toshortdatestring(),     dateend = x.dateend.toshortdatestring() }); 

you have options also:

  • if know want list or array, can use tolist() or toarray() instead of asenumerable().

  • if want resulting string in specific format, or use specific culture, can use tostring instead of toshortdatestring

for example, might want array containing standard iso-8601 date string , might want use invariant culture avoid side effect when current culture uses non-gregorian calendar system.

var results = query.toarray().select(x=> new {     idobjective = x.idobjective,     objectivename = x.objectivename,     datecreation = x.datecreation.tostring("yyyy-mm-dd", cultureinfo.invariantculture),     dateend = x.dateend.tostring("yyyy-mm-dd", cultureinfo.invariantculture) }); 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -