Question

Quelle est la syntaxe eSQL correcte pour comparer les dates?

L'expression équivalente LINQ-to-Entities ressemblerait à ceci:

var lastYearsShipments = from p in MyDataServiceContext.Products
                         where p.ShipDate.Value.Year == DateTime.Now.Year - 1
                         select p;
Était-ce utile?

La solution

Ils disent qu'il est acceptable de répondre à votre propre question , alors voilà ...

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>

Voir aussi: documentation sur msdn

Autres conseils

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); 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top