I'm trying to write a query who select all data who was created today.
Here is my request :
public List<Projet> GetEnvoiMailBienvenueProjet(Site site)
{
var res =
_context.Projet.Where(
a =>
a.SiteId == site.SiteId && a.Client1.AdresseClient.EnvoiEmailBienvenue &&
a.Client1.AdresseClient.Email != null && a.Client1.AdresseClient.Email != "" &&
a.CreationDate.ToString("{0:MM/dd/yyyy}") == DateTime.Now.ToString("{0:MM/dd/yyyy}"));
return res.ToList();
}
I handle this exception :
{"Method 'System.String ToString (System.String)' does not support the translation into SQL."}
I already try others possibility all time throw an exception like that.
I would recommend that you get rid of the string conversion entirely:
DateTime today = DateTime.Today;
var res =
_context.Projet.Where(
a => ... && a.CreationDate == today);
Or possibly (if a.CreationDate
contains a time as well):
DateTime today = DateTime.Today;
var res =
_context.Projet.Where(
a => ... && a.CreationDate.Date == today);
You should only use string conversions (either parsing or formatting) when you're actually interested in the textual representation. Here, it seems that all you're really interested in is comparing the date components - and you definitely don't need string conversions for that.
See more on this question at Stackoverflow