Pregunta

¿Cuál es la sintaxis correcta de eSQL para comparar fechas?

La expresión equivalente de LINQ-to-Entities se vería así:

var lastYearsShipments = from p in MyDataServiceContext.Products
                         where p.ShipDate.Value.Year == DateTime.Now.Year - 1
                         select p;
¿Fue útil?

Solución

Dicen que es aceptable para responder a su propia pregunta , así que aquí va ...

var predicate = string.Format(
    "Year(it.ShipDate) == Year(cast('{0}' as System.DateTime)) -1",
    System.DateTime.Now);

var lastYearsShipments = 
    myQuery.Products.Where(predicate); // myQuery is type ObjectQuery<T>

Consulte también: documentación de msdn

Otros consejos

string datetimeFormatter = "yyyy-MM-dd HH:mm";

var predicate = string.Format("Year(it.ShipDate) == Year(DATETIME'{0}')) -1", System.DateTime.Now.ToString(datetimeFormatter)); 

var lastYearsShipments = myQuery.Products.Where(predicate); 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top