Lets say you have the following linq expression:
from o in salesEntities.Orders where o.OrderDate < DateTime.Today.AddDays(-20) select o
Entity Framework does not know how to translate DateTime.Today.AddDays(-20) into an Entity SQL expression, and you get the following error:
LINQ to Entities does not recognize the method 'System.DateTime AddDays(Double)' method, and this method cannot be translated into a store expression.
So here is my questions: Is there any way to get Linq to Entities to evaluate part of the lambda expression and substitute a constant value, without me having to declare a local variable to hold it?
LINQ-to-Entities can deal with local values but not local expressions. Your code will work with this minor change:
var pastDate = DateTime.Today.AddDays(-20);
from o in salesEntities.Orders where o.OrderDate < pastDate select o
Change your expression using the EntityFunction for add days like this:
var result =
(
from o in salesEntities.Orders
where o.OrderDate < System.Data.Objects.EntityFunctions.AddDays(DateTime.Today,-20)
select o
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With