c# - LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' -
in linq want make thw follow query:
_tarearepositorio.obtenertodo() .where( x => convert.todatetime(x.fechacreacion.tostring("d")) >= fechainicial && convert.todatetime(x.fechacreacion.tostring("d")) <= fechafinal).tolist(); but, @ moment of execute query appears follow mistake:
linq entities not recognize method 'system.datetime todatetime(system.string)' method, , method cannot translated store expression.
how can fix ?
edit
the trouble need compare date, not time ...
try this
edit: add dbfunctions.truncatetime() desired effect
_tarearepositorio.obtenertodo() .where( x => dbfunctions.truncatetime(x.fechacreacion) >= fechainicial && dbfunctions.truncatetime(x.fechacreacion) <= fechafinal) .tolist(); the exception getting because convert.todatetime .net method cannot converted sql. should done after query has been materialized (i.e. using .tolist() before where) in particular case unnecesary since datetime objects can compared using >= , <= , entity framework can convert sql successfully
now if want compare date part of datetime can use method dbfunctions.truncatetime() lives inside system.data.entity namespace. method allow ef correctly truncate date in sql
Comments
Post a Comment