I am working with a Nurse calendar that consists of Shifts:
public interface IShift
{
ShiftType ShiftType { get; } //enum {Day, Early, Late, Night}
DateTime Day { get; }
bool IsNightShift();
bool IsWeekendShift();
}
The Nurse calendar is IEnumerable<IShift>. From that, I am selecting my own shifts (IEnumerable<IShift> selectedShifts) for a shorter period of time to check for some constraints.
I am trying to count multiple conditions, for example, Night Shifts on Friday:
var countFridayNight = selectedShifts.Count(s => s.IsNightShift()
&& s.Day.DayOfWeek == DayOfWeek.Friday);
What I am struggling with is to count multiple things on multiple days. For example Late shift on Friday and Early shift the next Monday. I have tried this, but VS doesn't seem to like the syntax:
var countFridayLateAndMondayEarly =
selectedShifts.Count(
(r, s) => s.ShiftType == ShiftType.Late && s.Day.DayOfWeek == DayOfWeek.Friday
&& r.Day.DayOfWeek == DayOfWeek.Monday && r.Day.AddDays(-2).DayOfYear == s.Day.DayOfYear && r.ShiftType == ShiftType.Early
);
Edit: Removed curly braces in the last lambda expression.
Edit2: There were two comments saying that Count can't take more than one variable inside the lambda expression. How else can I do what I need to using LINQ?
Edit3: Clarification of the problem - I need to count the shifts that are Late shifts on Friday and at the same time, there exists another shift that is Early the next Monday.
randsparameters? The.Count()extension method takes a predicate - it should have one parameter that is the type of your collection item, and should return bool - whether to include it into the count or not. I.e. ifselectedShiftsis anIEnumerable<IShift>then count should take a singleIShiftand return abool..Count()which is clearly not working