From 6ea3d8c127eafbcf9d1b6e9338b737e91e256875 Mon Sep 17 00:00:00 2001 From: Stepan Goremykin Date: Sat, 1 Apr 2023 16:57:19 +0200 Subject: [PATCH] Use MinBy and MaxBy instead of OrderBy + First --- src/NzbDrone.Common/Disk/DiskProviderBase.cs | 5 ++--- .../Download/Pending/PendingReleaseService.cs | 6 ++---- src/NzbDrone.Core/History/HistoryRepository.cs | 8 ++------ src/NzbDrone.Core/RootFolders/RootFolderService.cs | 4 +--- .../SeriesStats/SeriesStatisticsService.cs | 13 ++++--------- src/NzbDrone.Core/Tv/ShouldRefreshSeries.cs | 2 +- 6 files changed, 12 insertions(+), 26 deletions(-) diff --git a/src/NzbDrone.Common/Disk/DiskProviderBase.cs b/src/NzbDrone.Common/Disk/DiskProviderBase.cs index 42eccff47..283162bfd 100644 --- a/src/NzbDrone.Common/Disk/DiskProviderBase.cs +++ b/src/NzbDrone.Common/Disk/DiskProviderBase.cs @@ -450,12 +450,11 @@ namespace NzbDrone.Common.Disk return mounts.Where(drive => drive.RootDirectory.PathEquals(path) || drive.RootDirectory.IsParentPath(path)) - .OrderByDescending(drive => drive.RootDirectory.Length) - .FirstOrDefault(); + .MaxBy(drive => drive.RootDirectory.Length); } catch (Exception ex) { - Logger.Debug(ex, string.Format("Failed to get mount for path {0}", path)); + Logger.Debug(ex, $"Failed to get mount for path {path}"); return null; } } diff --git a/src/NzbDrone.Core/Download/Pending/PendingReleaseService.cs b/src/NzbDrone.Core/Download/Pending/PendingReleaseService.cs index 7f59e83fe..58ac71464 100644 --- a/src/NzbDrone.Core/Download/Pending/PendingReleaseService.cs +++ b/src/NzbDrone.Core/Download/Pending/PendingReleaseService.cs @@ -10,7 +10,6 @@ using NzbDrone.Core.DecisionEngine; using NzbDrone.Core.Download.Aggregation; using NzbDrone.Core.Indexers; using NzbDrone.Core.Jobs; -using NzbDrone.Core.Languages; using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Parser; using NzbDrone.Core.Parser.Model; @@ -97,7 +96,7 @@ namespace NzbDrone.Core.Download.Pending var episodeIds = decision.RemoteEpisode.Episodes.Select(e => e.Id); - var existingReports = episodeIds.SelectMany(v => alreadyPendingByEpisode[v] ?? Enumerable.Empty()) + var existingReports = episodeIds.SelectMany(v => alreadyPendingByEpisode[v]) .Distinct().ToList(); var matchingReports = existingReports.Where(MatchingReleasePredicate(decision.RemoteEpisode.Release)).ToList(); @@ -243,8 +242,7 @@ namespace NzbDrone.Core.Download.Pending return seriesReleases.Select(r => r.RemoteEpisode) .Where(r => r.Episodes.Select(e => e.Id).Intersect(episodeIds).Any()) - .OrderByDescending(p => p.Release.AgeHours) - .FirstOrDefault(); + .MaxBy(p => p.Release.AgeHours); } private ILookup CreateEpisodeLookup(IEnumerable alreadyPending) diff --git a/src/NzbDrone.Core/History/HistoryRepository.cs b/src/NzbDrone.Core/History/HistoryRepository.cs index b9326cd58..28774e865 100644 --- a/src/NzbDrone.Core/History/HistoryRepository.cs +++ b/src/NzbDrone.Core/History/HistoryRepository.cs @@ -30,9 +30,7 @@ namespace NzbDrone.Core.History public EpisodeHistory MostRecentForEpisode(int episodeId) { - return Query(h => h.EpisodeId == episodeId) - .OrderByDescending(h => h.Date) - .FirstOrDefault(); + return Query(h => h.EpisodeId == episodeId).MaxBy(h => h.Date); } public List FindByEpisodeId(int episodeId) @@ -44,9 +42,7 @@ namespace NzbDrone.Core.History public EpisodeHistory MostRecentForDownloadId(string downloadId) { - return Query(h => h.DownloadId == downloadId) - .OrderByDescending(h => h.Date) - .FirstOrDefault(); + return Query(h => h.DownloadId == downloadId).MaxBy(h => h.Date); } public List FindByDownloadId(string downloadId) diff --git a/src/NzbDrone.Core/RootFolders/RootFolderService.cs b/src/NzbDrone.Core/RootFolders/RootFolderService.cs index 89ebc0adf..63700f87d 100644 --- a/src/NzbDrone.Core/RootFolders/RootFolderService.cs +++ b/src/NzbDrone.Core/RootFolders/RootFolderService.cs @@ -186,9 +186,7 @@ namespace NzbDrone.Core.RootFolders public string GetBestRootFolderPath(string path) { - var possibleRootFolder = All().Where(r => r.Path.IsParentPath(path)) - .OrderByDescending(r => r.Path.Length) - .FirstOrDefault(); + var possibleRootFolder = All().Where(r => r.Path.IsParentPath(path)).MaxBy(r => r.Path.Length); if (possibleRootFolder == null) { diff --git a/src/NzbDrone.Core/SeriesStats/SeriesStatisticsService.cs b/src/NzbDrone.Core/SeriesStats/SeriesStatisticsService.cs index 2914a4216..44b5ddc16 100644 --- a/src/NzbDrone.Core/SeriesStats/SeriesStatisticsService.cs +++ b/src/NzbDrone.Core/SeriesStats/SeriesStatisticsService.cs @@ -50,16 +50,11 @@ namespace NzbDrone.Core.SeriesStats ReleaseGroups = seasonStatistics.SelectMany(s => s.ReleaseGroups).Distinct().ToList() }; - var nextAiring = seasonStatistics.Where(s => s.NextAiring != null) - .OrderBy(s => s.NextAiring) - .FirstOrDefault(); + var nextAiring = seasonStatistics.Where(s => s.NextAiring != null).MinBy(s => s.NextAiring); + var previousAiring = seasonStatistics.Where(s => s.PreviousAiring != null).MaxBy(s => s.PreviousAiring); - var previousAiring = seasonStatistics.Where(s => s.PreviousAiring != null) - .OrderBy(s => s.PreviousAiring) - .LastOrDefault(); - - seriesStatistics.NextAiringString = nextAiring != null ? nextAiring.NextAiringString : null; - seriesStatistics.PreviousAiringString = previousAiring != null ? previousAiring.PreviousAiringString : null; + seriesStatistics.NextAiringString = nextAiring?.NextAiringString; + seriesStatistics.PreviousAiringString = previousAiring?.PreviousAiringString; return seriesStatistics; } diff --git a/src/NzbDrone.Core/Tv/ShouldRefreshSeries.cs b/src/NzbDrone.Core/Tv/ShouldRefreshSeries.cs index ebeafc3ef..7918d1ba0 100644 --- a/src/NzbDrone.Core/Tv/ShouldRefreshSeries.cs +++ b/src/NzbDrone.Core/Tv/ShouldRefreshSeries.cs @@ -40,7 +40,7 @@ namespace NzbDrone.Core.Tv return true; } - var lastEpisode = _episodeService.GetEpisodeBySeries(series.Id).OrderByDescending(e => e.AirDateUtc).FirstOrDefault(); + var lastEpisode = _episodeService.GetEpisodeBySeries(series.Id).MaxBy(e => e.AirDateUtc); if (lastEpisode != null && lastEpisode.AirDateUtc > DateTime.UtcNow.AddDays(-30)) {