I am new to C# world and Entity Framework.
I need to retrieve items from a table which are over due in 30 days. I am aware of the filtering I can do on the SQL side (with a stored procedure), but I am looking for a solution in Entity Framework.
This is my current code:
using (var db = new MyDbContext())
{
var deliverableItems = db.Deliverables
.ToList() // fetch all records from Deliverable table.
.Where(d => Is30DaysDue(d));
foreach (var deliverable in deliverableItems)
{
Console.WriteLine(deliverable.DeliverableTitle);
// perform action on due items....
}
}
internal static bool Is30DaysDue(Deliverable deliverable)
{
var deliverableDueDate = deliverable.DeliverableRevisedDueDate ?? deliverable.DeliverableDueDate.Date;
var dateDiff = (deliverableDueDate.Date - DateTime.Now.Date).TotalDays;
return dateDiff == 30;
}
As you can see in line 4, I am first retrieving all records from the Deliverables
table and then doing the required filtering.
Is there any way to fetch 30 days due items using Entity Framework?
Edit:
Forgot to mention before, I have two dates DeliverableRevisedDueDate
and DeliverableDueDate
. If revised due date is null only then deliverable due date is considered.