❔ How to avoid multiple loop of collection?
static public Intervals SplitIntervals( List<WorktimeScheduleInterval> intervals )
{
var workdayIntervals = intervals
.Where( interval => interval.IntervalType != ShiftIntervalType.Break
&& interval.IntervalType != ShiftIntervalType.EmptyType )
.SelectMany( interval => new[]
{
interval.TimeShift,
interval.TimeShift + interval.Duration
} )
.OrderBy( ts => ts )
.Zip( intervals
.Where( interval => interval.IntervalType != ShiftIntervalType.Break
&& interval.IntervalType != ShiftIntervalType.EmptyType )
.SelectMany( interval => new[]
{
interval.TimeShift,
interval.TimeShift + interval.Duration
} )
.OrderBy( ts => ts )
.Skip( 1 ),
( start, end ) => (
Start: start,
End: end) );
var breakIntervals = intervals
.Where( interval => interval.IntervalType == ShiftIntervalType.Break )
.Select( interval => (
Start: interval.TimeShift,
End: interval.TimeShift + interval.Duration) );
var emptyIntervals = intervals
.Where( interval => interval.IntervalType == ShiftIntervalType.EmptyType )
.Select( interval => (
Start: TimeSpan.FromDays( 0 ),
End: TimeSpan.FromDays( 0 )) );
return new Intervals(
workdayIntervals,
breakIntervals,
emptyIntervals );
}
static public Intervals SplitIntervals( List<WorktimeScheduleInterval> intervals )
{
var workdayIntervals = intervals
.Where( interval => interval.IntervalType != ShiftIntervalType.Break
&& interval.IntervalType != ShiftIntervalType.EmptyType )
.SelectMany( interval => new[]
{
interval.TimeShift,
interval.TimeShift + interval.Duration
} )
.OrderBy( ts => ts )
.Zip( intervals
.Where( interval => interval.IntervalType != ShiftIntervalType.Break
&& interval.IntervalType != ShiftIntervalType.EmptyType )
.SelectMany( interval => new[]
{
interval.TimeShift,
interval.TimeShift + interval.Duration
} )
.OrderBy( ts => ts )
.Skip( 1 ),
( start, end ) => (
Start: start,
End: end) );
var breakIntervals = intervals
.Where( interval => interval.IntervalType == ShiftIntervalType.Break )
.Select( interval => (
Start: interval.TimeShift,
End: interval.TimeShift + interval.Duration) );
var emptyIntervals = intervals
.Where( interval => interval.IntervalType == ShiftIntervalType.EmptyType )
.Select( interval => (
Start: TimeSpan.FromDays( 0 ),
End: TimeSpan.FromDays( 0 )) );
return new Intervals(
workdayIntervals,
breakIntervals,
emptyIntervals );
}
workdayIntervals, breakIntervals, emptyIntervals
just for one loop2 Replies