I am building an ASP.Net MVC 3 Web application which uses Entity Framework 4.1. I also use the Unit of Work pattern with a Generic Repository. I have two Entities in my Model, Shift and ShiftDate. Please see the diagram below for their structure. The idea is, a Shift can have 1 or many ShiftDates, and a ShiftDate can belong to only one Shift.

I wish to create a query which will return a List of all Shifts like so
public IList<Shift> GetShifts(int id)
{
return _UoW.Shifts.Get(s => s.organisationID == id).OrderBy(s => s.startDate).ToList();
}
Then in my Razor View, I wish to use a foreach loop and list the details of each Shift. One of these details is the total number of ShiftDates each Shift has. To do this I will do something like
foreach(var shiftDate in Model.Shifts)
{
@Html.DisplayFor(modelItem => item.ShiftDates.Count)
}
This works fine, however, I want the count to exclude ShiftDates whose shiftDateStatusID is set to 442. To do this I tried amending my Linq Query to this
return _UoW.Shifts.Get(s => s.organisationID == id && s.ShiftDates.Any(sd => sd.shiftDateStatusID != 442))
.OrderBy(s => s.startDate).ToList();
However, this still pulls back ShiftDates who have a shiftDateStatusID of 442.
Hopefully this makes sense.
Any feedback would be greatly appreciated.
Thanks.
If you do
you get shifts that have at least one
ShiftDatehaving ashiftDateStatusIDother than 442. Well, there are probably a lot of shifts meeting this condition. TheShiftDatescollections of these shifts will be loaded completely, including those with 442 (if any).Either you have to exclude the
ShiftDates with status 442 when you populate your view model, or when it’s only for the count, exclude them when you do the count: