I have a table AppointmentStatusHistory in the following format:
AppointmentId AppointmentStatusId Date
========================================================
1 1 2/1/2012 (2nd January)
1 2 2/2/2012 (2nd February)
I currently run a query against this to return the ‘most recent’ status for the appointment within a given timeframe.
My LINQ Query
items = (from t1 in db.AppointmentStatusHistories
from t2 in db.TaskAppointments
.Where(o => (o.ID == t1.AppointmentId))
from t3 in db.Tasks
.Where(o => (o.Task_ID == t2.Parent_Task_ID))
where t1.Timestamp >= first && t1.Timestamp <= last
&& t3.Creator == user
group t1 by t1.AppointmentId into grp
select new UserAppointmentTargetModel
{
AppointmentId = grp.Key,
AppointmentStatusId = grp.OrderByDescending(g => g.Timestamp)
.FirstOrDefault()
.AppointmentStatusId
}
);
Using the above returns AppointmentStatusId status of ‘1’ when first=1/1/2012 and last=2/1/2012.
Requirements
I hoped somebody may be able to give me some advice on amending this to meet the following conditions:
- If the most up to date status is within the current period, include the record.
- If not, omit it from the result set.
You just need to move the
lastpart of the filtering to after the grouping/winnerpicking.(obligatory query comprehension syntax form of the above)
Side notes:
I used navigation properties to do the user filtering. If you can’t get that to work, go back to the joins.
It’s always safe to call First on a Group. Groups aren’t ever empty. There’s no need for FirstOrDefault in this case.
I reused the
ashvariable name in the method style query to communicate type since it is declared in two different places where the type is not stated. I changed that towinnerin the comprehension style query to communicate intent better since it is declared in one place where the type may be verified by inspection.Also, I never use >= with dates. It can only lead to sorrow.