C
C#2y ago
SWEETPONY

❔ 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 );
}
I don't like that I use select 3 times. I don't understand how to get workdayIntervals, breakIntervals, emptyIntervals just for one loop
2 Replies
pox
pox2y ago
The empty one seems abit wierd btw, its always gonna be a list of (TimeSpan.Zero, TimeSpan.Zero)
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server