LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)'
Try this
EDIT: Add DbFunctions.TruncateTime()
to get the desired effect
_tareaRepositorio.ObtenerTodo()
.Where( x => DbFunctions.TruncateTime(x.FechaCreacion) >= fechaInicial &&
DbFunctions.TruncateTime(x.FechaCreacion) <= fechaFinal)
.ToList();
The exception you are getting is because the Convert.ToDateTime
is a .NET method that cannot be converted into SQL. Normally this should be done after the query has been materialized (i.e. using .ToList()
before the Where
) but in your particular case it is unnecesary since DateTime
objects can be compared using >=
and <=
and Entity Framework can convert it to SQL successfully
Now if you only want to compare the Date part of the Datetime you can use the method DbFunctions.TruncateTime()
which lives inside System.Data.Entity
namespace. This method will allow EF to correctly truncate the date in SQL