Ravi Ram
Ravi Ram

Reputation: 24488

Filter by Date Difference in LINQ using EF

I need to filter a record set like

OrderShippedDate - 20 days <-- Get all orders with a ShippedDate 20 days ago

var orders = ctx.Orders.Where(p => p.OrderShippedDate == 20) <---??? not sure what I need here .ToList();

How do I do a date diff in EF / LINQ?

Upvotes: 0

Views: 1278

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500175

I would suggest you work out your parameters locally, and then pass those in. It's not clear from your description whether you mean exactly 20 days ago, more than 20 days ago, or less than 20 days ago, which makes it hard to give you concrete advice, but if it's "more than 20 days ago" you might use something like:

var upperBound = DateTime.Today.AddDays(-20);
var orders = ctx.Orders.Where(p => p.OrderShippedDate < upperBound);

Upvotes: 2

Related Questions