Use MinBy and MaxBy instead of OrderBy + First

This commit is contained in:
Stepan Goremykin 2023-04-01 16:57:19 +02:00 committed by Mark McDowall
parent 7fedfe7423
commit 6ea3d8c127
6 changed files with 12 additions and 26 deletions

View File

@ -450,12 +450,11 @@ namespace NzbDrone.Common.Disk
return mounts.Where(drive => drive.RootDirectory.PathEquals(path) || return mounts.Where(drive => drive.RootDirectory.PathEquals(path) ||
drive.RootDirectory.IsParentPath(path)) drive.RootDirectory.IsParentPath(path))
.OrderByDescending(drive => drive.RootDirectory.Length) .MaxBy(drive => drive.RootDirectory.Length);
.FirstOrDefault();
} }
catch (Exception ex) 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; return null;
} }
} }

View File

@ -10,7 +10,6 @@ using NzbDrone.Core.DecisionEngine;
using NzbDrone.Core.Download.Aggregation; using NzbDrone.Core.Download.Aggregation;
using NzbDrone.Core.Indexers; using NzbDrone.Core.Indexers;
using NzbDrone.Core.Jobs; using NzbDrone.Core.Jobs;
using NzbDrone.Core.Languages;
using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Parser; using NzbDrone.Core.Parser;
using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Parser.Model;
@ -97,7 +96,7 @@ namespace NzbDrone.Core.Download.Pending
var episodeIds = decision.RemoteEpisode.Episodes.Select(e => e.Id); var episodeIds = decision.RemoteEpisode.Episodes.Select(e => e.Id);
var existingReports = episodeIds.SelectMany(v => alreadyPendingByEpisode[v] ?? Enumerable.Empty<PendingRelease>()) var existingReports = episodeIds.SelectMany(v => alreadyPendingByEpisode[v])
.Distinct().ToList(); .Distinct().ToList();
var matchingReports = existingReports.Where(MatchingReleasePredicate(decision.RemoteEpisode.Release)).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) return seriesReleases.Select(r => r.RemoteEpisode)
.Where(r => r.Episodes.Select(e => e.Id).Intersect(episodeIds).Any()) .Where(r => r.Episodes.Select(e => e.Id).Intersect(episodeIds).Any())
.OrderByDescending(p => p.Release.AgeHours) .MaxBy(p => p.Release.AgeHours);
.FirstOrDefault();
} }
private ILookup<int, PendingRelease> CreateEpisodeLookup(IEnumerable<PendingRelease> alreadyPending) private ILookup<int, PendingRelease> CreateEpisodeLookup(IEnumerable<PendingRelease> alreadyPending)

View File

@ -30,9 +30,7 @@ namespace NzbDrone.Core.History
public EpisodeHistory MostRecentForEpisode(int episodeId) public EpisodeHistory MostRecentForEpisode(int episodeId)
{ {
return Query(h => h.EpisodeId == episodeId) return Query(h => h.EpisodeId == episodeId).MaxBy(h => h.Date);
.OrderByDescending(h => h.Date)
.FirstOrDefault();
} }
public List<EpisodeHistory> FindByEpisodeId(int episodeId) public List<EpisodeHistory> FindByEpisodeId(int episodeId)
@ -44,9 +42,7 @@ namespace NzbDrone.Core.History
public EpisodeHistory MostRecentForDownloadId(string downloadId) public EpisodeHistory MostRecentForDownloadId(string downloadId)
{ {
return Query(h => h.DownloadId == downloadId) return Query(h => h.DownloadId == downloadId).MaxBy(h => h.Date);
.OrderByDescending(h => h.Date)
.FirstOrDefault();
} }
public List<EpisodeHistory> FindByDownloadId(string downloadId) public List<EpisodeHistory> FindByDownloadId(string downloadId)

View File

@ -186,9 +186,7 @@ namespace NzbDrone.Core.RootFolders
public string GetBestRootFolderPath(string path) public string GetBestRootFolderPath(string path)
{ {
var possibleRootFolder = All().Where(r => r.Path.IsParentPath(path)) var possibleRootFolder = All().Where(r => r.Path.IsParentPath(path)).MaxBy(r => r.Path.Length);
.OrderByDescending(r => r.Path.Length)
.FirstOrDefault();
if (possibleRootFolder == null) if (possibleRootFolder == null)
{ {

View File

@ -50,16 +50,11 @@ namespace NzbDrone.Core.SeriesStats
ReleaseGroups = seasonStatistics.SelectMany(s => s.ReleaseGroups).Distinct().ToList() ReleaseGroups = seasonStatistics.SelectMany(s => s.ReleaseGroups).Distinct().ToList()
}; };
var nextAiring = seasonStatistics.Where(s => s.NextAiring != null) var nextAiring = seasonStatistics.Where(s => s.NextAiring != null).MinBy(s => s.NextAiring);
.OrderBy(s => s.NextAiring) var previousAiring = seasonStatistics.Where(s => s.PreviousAiring != null).MaxBy(s => s.PreviousAiring);
.FirstOrDefault();
var previousAiring = seasonStatistics.Where(s => s.PreviousAiring != null) seriesStatistics.NextAiringString = nextAiring?.NextAiringString;
.OrderBy(s => s.PreviousAiring) seriesStatistics.PreviousAiringString = previousAiring?.PreviousAiringString;
.LastOrDefault();
seriesStatistics.NextAiringString = nextAiring != null ? nextAiring.NextAiringString : null;
seriesStatistics.PreviousAiringString = previousAiring != null ? previousAiring.PreviousAiringString : null;
return seriesStatistics; return seriesStatistics;
} }

View File

@ -40,7 +40,7 @@ namespace NzbDrone.Core.Tv
return true; 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)) if (lastEpisode != null && lastEpisode.AirDateUtc > DateTime.UtcNow.AddDays(-30))
{ {