public VehicleNLapAverage BestNLaps(int targetLapCount) { if (LapCount < targetLapCount) { return(null); } else { VehicleNLapAverage average = null; for (int lapIndex = 0; lapIndex < LapCount - targetLapCount; lapIndex++) { var laps = VehicleLapEvents.OrderBy(l => l.Elapsed).Skip(lapIndex).Take(targetLapCount); var lapAverage = laps.Average(l => l.LapTime); if (average == null || lapAverage < average.AverageLapTime) { average = new VehicleNLapAverage() { VehicleId = VehicleId, StartLap = laps.Min(l => l.LapNumber), EndLap = laps.Max(l => l.LapNumber), LapCount = targetLapCount, AverageLapTime = laps.Average(l => l.LapTime) }; } } return(average); } }
public VehicleNLapAverage LastNLaps(int targetLapCount) { if (LapCount < targetLapCount) { return(null); } else { var laps = VehicleLapEvents.OrderByDescending(l => l.Elapsed).Take(targetLapCount); VehicleNLapAverage average = new VehicleNLapAverage() { VehicleId = VehicleId, StartLap = laps.Min(l => l.LapNumber), EndLap = laps.Max(l => l.LapNumber), LapCount = targetLapCount, AverageLapTime = laps.Average(l => l.LapTime) }; return(average); } }