Use MinBy and MaxBy instead of OrderBy + First

(cherry picked from commit 6ea3d8c127eafbcf9d1b6e9338b737e91e256875)

Closes #3485
pull/3610/head
Stepan Goremykin 2 years ago committed by Bogdan
parent 8dbc522774
commit 565c05c4c9

@ -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;
}
}

@ -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<PendingRelease>())
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<PendingRelease> GetPendingReleases()

@ -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<EntityHistory> FindByDownloadId(string downloadId)

@ -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 ||

@ -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");

@ -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;

@ -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;
}

@ -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))
{

@ -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)

Loading…
Cancel
Save