From 565c05c4c976453cf0bb69af8626f564017ed479 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 (cherry picked from commit 6ea3d8c127eafbcf9d1b6e9338b737e91e256875) Closes #3485 --- src/NzbDrone.Common/Disk/DiskProviderBase.cs | 5 ++--- .../Download/Pending/PendingReleaseService.cs | 5 ++--- src/NzbDrone.Core/History/EntityHistoryRepository.cs | 8 ++------ .../TrackImport/Identification/IdentificationService.cs | 3 +-- .../Specifications/CloseAlbumMatchSpecification.cs | 2 +- src/NzbDrone.Core/MetadataSource/SkyHook/SkyHookProxy.cs | 2 +- .../Music/Services/MonitorNewAlbumService.cs | 2 +- src/NzbDrone.Core/Music/Utilities/ShouldRefreshArtist.cs | 2 +- src/NzbDrone.Core/RootFolders/RootFolderService.cs | 3 +-- 9 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/NzbDrone.Common/Disk/DiskProviderBase.cs b/src/NzbDrone.Common/Disk/DiskProviderBase.cs index 8cf223941..2cf49f1c2 100644 --- a/src/NzbDrone.Common/Disk/DiskProviderBase.cs +++ b/src/NzbDrone.Common/Disk/DiskProviderBase.cs @@ -473,12 +473,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 f73bebfc7..5828664d0 100644 --- a/src/NzbDrone.Core/Download/Pending/PendingReleaseService.cs +++ b/src/NzbDrone.Core/Download/Pending/PendingReleaseService.cs @@ -96,7 +96,7 @@ namespace NzbDrone.Core.Download.Pending var albumIds = decision.RemoteAlbum.Albums.Select(e => e.Id); - var existingReports = albumIds.SelectMany(v => alreadyPendingByAlbum[v] ?? Enumerable.Empty()) + var existingReports = albumIds.SelectMany(v => alreadyPendingByAlbum[v]) .Distinct().ToList(); var matchingReports = existingReports.Where(MatchingReleasePredicate(decision.RemoteAlbum.Release)).ToList(); @@ -253,8 +253,7 @@ namespace NzbDrone.Core.Download.Pending return artistReleases.Select(r => r.RemoteAlbum) .Where(r => r.Albums.Select(e => e.Id).Intersect(albumIds).Any()) - .OrderByDescending(p => p.Release.AgeHours) - .FirstOrDefault(); + .MaxBy(p => p.Release.AgeHours); } private List GetPendingReleases() diff --git a/src/NzbDrone.Core/History/EntityHistoryRepository.cs b/src/NzbDrone.Core/History/EntityHistoryRepository.cs index 24d0ac4b3..4491bed11 100644 --- a/src/NzbDrone.Core/History/EntityHistoryRepository.cs +++ b/src/NzbDrone.Core/History/EntityHistoryRepository.cs @@ -29,16 +29,12 @@ namespace NzbDrone.Core.History public EntityHistory MostRecentForAlbum(int albumId) { - return Query(h => h.AlbumId == albumId) - .OrderByDescending(h => h.Date) - .FirstOrDefault(); + return Query(h => h.AlbumId == albumId).MaxBy(h => h.Date); } public EntityHistory 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/MediaFiles/TrackImport/Identification/IdentificationService.cs b/src/NzbDrone.Core/MediaFiles/TrackImport/Identification/IdentificationService.cs index 71d9039c8..9db887793 100644 --- a/src/NzbDrone.Core/MediaFiles/TrackImport/Identification/IdentificationService.cs +++ b/src/NzbDrone.Core/MediaFiles/TrackImport/Identification/IdentificationService.cs @@ -155,8 +155,7 @@ namespace NzbDrone.Core.MediaFiles.TrackImport.Identification private bool ShouldFingerprint(LocalAlbumRelease localAlbumRelease) { var worstTrackMatchDist = localAlbumRelease.TrackMapping?.Mapping - .OrderByDescending(x => x.Value.Item2.NormalizedDistance()) - .First() + .MaxBy(x => x.Value.Item2.NormalizedDistance()) .Value.Item2.NormalizedDistance() ?? 1.0; if (localAlbumRelease.Distance.NormalizedDistance() > 0.15 || diff --git a/src/NzbDrone.Core/MediaFiles/TrackImport/Specifications/CloseAlbumMatchSpecification.cs b/src/NzbDrone.Core/MediaFiles/TrackImport/Specifications/CloseAlbumMatchSpecification.cs index 0db4334d5..fda5da995 100644 --- a/src/NzbDrone.Core/MediaFiles/TrackImport/Specifications/CloseAlbumMatchSpecification.cs +++ b/src/NzbDrone.Core/MediaFiles/TrackImport/Specifications/CloseAlbumMatchSpecification.cs @@ -34,7 +34,7 @@ namespace NzbDrone.Core.MediaFiles.TrackImport.Specifications return Decision.Reject($"Album match is not close enough: {1 - dist:P1} vs {1 - _albumThreshold:P0} {reasons}"); } - var worstTrackMatch = item.LocalTracks.Where(x => x.Distance != null).OrderByDescending(x => x.Distance.NormalizedDistance()).FirstOrDefault(); + var worstTrackMatch = item.LocalTracks.Where(x => x.Distance != null).MaxBy(x => x.Distance.NormalizedDistance()); if (worstTrackMatch == null) { _logger.Debug($"No tracks matched"); diff --git a/src/NzbDrone.Core/MetadataSource/SkyHook/SkyHookProxy.cs b/src/NzbDrone.Core/MetadataSource/SkyHook/SkyHookProxy.cs index b1dcc1e6c..b9857cd08 100644 --- a/src/NzbDrone.Core/MetadataSource/SkyHook/SkyHookProxy.cs +++ b/src/NzbDrone.Core/MetadataSource/SkyHook/SkyHookProxy.cs @@ -461,7 +461,7 @@ namespace NzbDrone.Core.MetadataSource.SkyHook album.AlbumReleases = resource.Releases.Select(x => MapRelease(x, artistDict)).Where(x => x.TrackCount > 0).ToList(); // Monitor the release with most tracks - var mostTracks = album.AlbumReleases.Value.OrderByDescending(x => x.TrackCount).FirstOrDefault(); + var mostTracks = album.AlbumReleases.Value.MaxBy(x => x.TrackCount); if (mostTracks != null) { mostTracks.Monitored = true; diff --git a/src/NzbDrone.Core/Music/Services/MonitorNewAlbumService.cs b/src/NzbDrone.Core/Music/Services/MonitorNewAlbumService.cs index 767913f94..3d92b0634 100644 --- a/src/NzbDrone.Core/Music/Services/MonitorNewAlbumService.cs +++ b/src/NzbDrone.Core/Music/Services/MonitorNewAlbumService.cs @@ -33,7 +33,7 @@ namespace NzbDrone.Core.Music if (monitorNewItems == NewItemMonitorTypes.New) { - var newest = existingAlbums.OrderByDescending(x => x.ReleaseDate ?? DateTime.MinValue).FirstOrDefault()?.ReleaseDate ?? DateTime.MinValue; + var newest = existingAlbums.MaxBy(x => x.ReleaseDate ?? DateTime.MinValue)?.ReleaseDate ?? DateTime.MinValue; return (addedAlbum.ReleaseDate ?? DateTime.MinValue) >= newest; } diff --git a/src/NzbDrone.Core/Music/Utilities/ShouldRefreshArtist.cs b/src/NzbDrone.Core/Music/Utilities/ShouldRefreshArtist.cs index dc9fca5c7..8bc09484f 100644 --- a/src/NzbDrone.Core/Music/Utilities/ShouldRefreshArtist.cs +++ b/src/NzbDrone.Core/Music/Utilities/ShouldRefreshArtist.cs @@ -40,7 +40,7 @@ namespace NzbDrone.Core.Music return true; } - var lastAlbum = _albumService.GetAlbumsByArtist(artist.Id).OrderByDescending(e => e.ReleaseDate).FirstOrDefault(); + var lastAlbum = _albumService.GetAlbumsByArtist(artist.Id).MaxBy(e => e.ReleaseDate); if (lastAlbum != null && lastAlbum.ReleaseDate > DateTime.UtcNow.AddDays(-30)) { diff --git a/src/NzbDrone.Core/RootFolders/RootFolderService.cs b/src/NzbDrone.Core/RootFolders/RootFolderService.cs index 44ee3b0ba..a92020bb1 100644 --- a/src/NzbDrone.Core/RootFolders/RootFolderService.cs +++ b/src/NzbDrone.Core/RootFolders/RootFolderService.cs @@ -143,8 +143,7 @@ namespace NzbDrone.Core.RootFolders public RootFolder GetBestRootFolder(string path) { return All().Where(r => PathEqualityComparer.Instance.Equals(r.Path, path) || r.Path.IsParentPath(path)) - .OrderByDescending(r => r.Path.Length) - .FirstOrDefault(); + .MaxBy(r => r.Path.Length); } public string GetBestRootFolderPath(string path)