diff --git a/Emby.Dlna/ContentDirectory/ControlHandler.cs b/Emby.Dlna/ContentDirectory/ControlHandler.cs index d22fc21774..4f74bb222e 100644 --- a/Emby.Dlna/ContentDirectory/ControlHandler.cs +++ b/Emby.Dlna/ContentDirectory/ControlHandler.cs @@ -425,10 +425,10 @@ namespace Emby.Dlna.ContentDirectory { var folder = (Folder)item; - var sortOrders = new List(); + var sortOrders = new List<(string, SortOrder)>(); if (!folder.IsPreSorted) { - sortOrders.Add(ItemSortBy.SortName); + sortOrders.Add((ItemSortBy.SortName, sort.SortOrder)); } var mediaTypes = new List(); @@ -464,7 +464,7 @@ namespace Emby.Dlna.ContentDirectory { Limit = limit, StartIndex = startIndex, - OrderBy = sortOrders.Select(i => new ValueTuple(i, sort.SortOrder)).ToArray(), + OrderBy = sortOrders, User = user, Recursive = true, IsMissing = false, @@ -872,10 +872,10 @@ namespace Emby.Dlna.ContentDirectory query.Parent = parent; query.SetUser(user); - query.OrderBy = new ValueTuple[] + query.OrderBy = new[] { - new ValueTuple (ItemSortBy.DatePlayed, SortOrder.Descending), - new ValueTuple (ItemSortBy.SortName, SortOrder.Ascending) + (ItemSortBy.DatePlayed, SortOrder.Descending), + (ItemSortBy.SortName, SortOrder.Ascending) }; query.IsResumable = true; @@ -1121,7 +1121,7 @@ namespace Emby.Dlna.ContentDirectory private QueryResult GetMusicLatest(BaseItem parent, User user, InternalItemsQuery query) { - query.OrderBy = new ValueTuple[] { }; + query.OrderBy = Array.Empty<(string, SortOrder)>(); var items = _userViewManager.GetLatestItems(new LatestItemsQuery { @@ -1138,7 +1138,7 @@ namespace Emby.Dlna.ContentDirectory private QueryResult GetNextUp(BaseItem parent, User user, InternalItemsQuery query) { - query.OrderBy = new ValueTuple[] { }; + query.OrderBy = Array.Empty<(string, SortOrder)>(); var result = _tvSeriesManager.GetNextUp(new NextUpQuery { @@ -1153,7 +1153,7 @@ namespace Emby.Dlna.ContentDirectory private QueryResult GetTvLatest(BaseItem parent, User user, InternalItemsQuery query) { - query.OrderBy = new ValueTuple[] { }; + query.OrderBy = Array.Empty<(string, SortOrder)>(); var items = _userViewManager.GetLatestItems(new LatestItemsQuery { @@ -1170,7 +1170,7 @@ namespace Emby.Dlna.ContentDirectory private QueryResult GetMovieLatest(BaseItem parent, User user, InternalItemsQuery query) { - query.OrderBy = new ValueTuple[] { }; + query.OrderBy = Array.Empty<(string, SortOrder)>(); var items = _userViewManager.GetLatestItems(new LatestItemsQuery { @@ -1274,13 +1274,14 @@ namespace Emby.Dlna.ContentDirectory private void SetSorting(InternalItemsQuery query, SortCriteria sort, bool isPreSorted) { - var sortOrders = new List(); - if (!isPreSorted) + if (isPreSorted) { - sortOrders.Add(ItemSortBy.SortName); + query.OrderBy = Array.Empty<(string, SortOrder)>(); + } + else + { + query.OrderBy = new[] { (ItemSortBy.SortName, sort.SortOrder) }; } - - query.OrderBy = sortOrders.Select(i => new ValueTuple(i, sort.SortOrder)).ToArray(); } private QueryResult ApplyPaging(QueryResult result, int? startIndex, int? limit) diff --git a/Emby.Server.Implementations/Channels/ChannelManager.cs b/Emby.Server.Implementations/Channels/ChannelManager.cs index 151670074a..8e955f1b0e 100644 --- a/Emby.Server.Implementations/Channels/ChannelManager.cs +++ b/Emby.Server.Implementations/Channels/ChannelManager.cs @@ -510,7 +510,7 @@ namespace Emby.Server.Implementations.Channels return _libraryManager.GetItemIds(new InternalItemsQuery { IncludeItemTypes = new[] { typeof(Channel).Name }, - OrderBy = new ValueTuple[] { new ValueTuple(ItemSortBy.SortName, SortOrder.Ascending) } + OrderBy = new[] { (ItemSortBy.SortName, SortOrder.Ascending) } }).Select(i => GetChannelFeatures(i.ToString("N", CultureInfo.InvariantCulture))).ToArray(); } @@ -618,16 +618,16 @@ namespace Emby.Server.Implementations.Channels { query.OrderBy = new[] { - new ValueTuple(ItemSortBy.PremiereDate, SortOrder.Descending), - new ValueTuple(ItemSortBy.ProductionYear, SortOrder.Descending), - new ValueTuple(ItemSortBy.DateCreated, SortOrder.Descending) + (ItemSortBy.PremiereDate, SortOrder.Descending), + (ItemSortBy.ProductionYear, SortOrder.Descending), + (ItemSortBy.DateCreated, SortOrder.Descending) }; } else { query.OrderBy = new[] { - new ValueTuple(ItemSortBy.DateCreated, SortOrder.Descending) + (ItemSortBy.DateCreated, SortOrder.Descending) }; } diff --git a/Emby.Server.Implementations/Collections/CollectionImageProvider.cs b/Emby.Server.Implementations/Collections/CollectionImageProvider.cs index 0244c4a684..bbb3114ca7 100644 --- a/Emby.Server.Implementations/Collections/CollectionImageProvider.cs +++ b/Emby.Server.Implementations/Collections/CollectionImageProvider.cs @@ -76,7 +76,6 @@ namespace Emby.Server.Implementations.Collections .Where(i => i != null) .GroupBy(x => x.Id) .Select(x => x.First()) - .OrderBy(i => Guid.NewGuid()) .ToList(); } diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs index 8d509f6888..69cfcb67b5 100644 --- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs @@ -7,6 +7,7 @@ using System.Text; using System.Text.Json; using System.Threading; using Emby.Server.Implementations.Playlists; +using MediaBrowser.Common.Extensions; using MediaBrowser.Common.Json; using MediaBrowser.Controller; using MediaBrowser.Controller.Channels; @@ -2831,8 +2832,8 @@ namespace Emby.Server.Implementations.Data BindSimilarParams(query, statement); BindSearchParams(query, statement); - // Running this again will bind the params - GetWhereClauses(query, statement); + // Running this again will bind the params + GetWhereClauses(query, statement); var hasEpisodeAttributes = HasEpisodeAttributes(query); var hasServiceName = HasServiceName(query); @@ -2881,14 +2882,14 @@ namespace Emby.Server.Implementations.Data private string GetOrderByText(InternalItemsQuery query) { + var orderBy = query.OrderBy; if (string.IsNullOrEmpty(query.SearchTerm)) { - int oldLen = query.OrderBy.Length; - - if (query.SimilarTo != null && oldLen == 0) + int oldLen = orderBy.Count; + if (oldLen == 0 && query.SimilarTo != null) { var arr = new (string, SortOrder)[oldLen + 2]; - query.OrderBy.CopyTo(arr, 0); + orderBy.CopyTo(arr, 0); arr[oldLen] = ("SimilarityScore", SortOrder.Descending); arr[oldLen + 1] = (ItemSortBy.Random, SortOrder.Ascending); query.OrderBy = arr; @@ -2896,16 +2897,15 @@ namespace Emby.Server.Implementations.Data } else { - query.OrderBy = new [] + query.OrderBy = new[] { ("SearchScore", SortOrder.Descending), (ItemSortBy.SortName, SortOrder.Ascending) }; } - var orderBy = query.OrderBy; - if (orderBy.Length == 0) + if (orderBy.Count == 0) { return string.Empty; } @@ -2913,14 +2913,8 @@ namespace Emby.Server.Implementations.Data return " ORDER BY " + string.Join(",", orderBy.Select(i => { var columnMap = MapOrderByField(i.Item1, query); - var columnAscending = i.Item2 == SortOrder.Ascending; - const bool enableOrderInversion = false; - if (columnMap.Item2 && enableOrderInversion) - { - columnAscending = !columnAscending; - } - var sortOrder = columnAscending ? "ASC" : "DESC"; + var sortOrder = i.Item2 == SortOrder.Ascending ? "ASC" : "DESC"; return columnMap.Item1 + " " + sortOrder; })); diff --git a/Emby.Server.Implementations/Library/LibraryManager.cs b/Emby.Server.Implementations/Library/LibraryManager.cs index 528636ecd3..826cdb9c6e 100644 --- a/Emby.Server.Implementations/Library/LibraryManager.cs +++ b/Emby.Server.Implementations/Library/LibraryManager.cs @@ -829,7 +829,7 @@ namespace Emby.Server.Implementations.Library { Path = path, IsFolder = isFolder, - OrderBy = new[] { ItemSortBy.DateCreated }.Select(i => new ValueTuple(i, SortOrder.Descending)).ToArray(), + OrderBy = new[] { (ItemSortBy.DateCreated, SortOrder.Descending) }, Limit = 1, DtoOptions = new DtoOptions(true) }; @@ -1257,7 +1257,7 @@ namespace Emby.Server.Implementations.Library public List GetItemList(InternalItemsQuery query, bool allowExternalContent) { - if (query.Recursive && !query.ParentId.Equals(Guid.Empty)) + if (query.Recursive && query.ParentId != Guid.Empty) { var parent = GetItemById(query.ParentId); if (parent != null) diff --git a/Emby.Server.Implementations/Library/MusicManager.cs b/Emby.Server.Implementations/Library/MusicManager.cs index 10602fea76..75cb67fcc1 100644 --- a/Emby.Server.Implementations/Library/MusicManager.cs +++ b/Emby.Server.Implementations/Library/MusicManager.cs @@ -89,10 +89,9 @@ namespace Emby.Server.Implementations.Library Limit = 200, - OrderBy = new[] { new ValueTuple(ItemSortBy.Random, SortOrder.Ascending) }, + OrderBy = new[] { (ItemSortBy.Random, SortOrder.Ascending) }, DtoOptions = dtoOptions - }); } diff --git a/Emby.Server.Implementations/Library/SearchEngine.cs b/Emby.Server.Implementations/Library/SearchEngine.cs index 6783b07eb8..c749aec424 100644 --- a/Emby.Server.Implementations/Library/SearchEngine.cs +++ b/Emby.Server.Implementations/Library/SearchEngine.cs @@ -162,7 +162,7 @@ namespace Emby.Server.Implementations.Library Limit = query.Limit, IncludeItemsByName = string.IsNullOrEmpty(query.ParentId), ParentId = string.IsNullOrEmpty(query.ParentId) ? Guid.Empty : new Guid(query.ParentId), - OrderBy = new[] { new ValueTuple(ItemSortBy.SortName, SortOrder.Ascending) }, + OrderBy = new[] { (ItemSortBy.SortName, SortOrder.Ascending) }, Recursive = true, IsKids = query.IsKids, diff --git a/Emby.Server.Implementations/Library/UserViewManager.cs b/Emby.Server.Implementations/Library/UserViewManager.cs index 9c99776429..a9b39c064e 100644 --- a/Emby.Server.Implementations/Library/UserViewManager.cs +++ b/Emby.Server.Implementations/Library/UserViewManager.cs @@ -340,7 +340,7 @@ namespace Emby.Server.Implementations.Library var query = new InternalItemsQuery(user) { IncludeItemTypes = includeItemTypes, - OrderBy = new[] { new ValueTuple(ItemSortBy.DateCreated, SortOrder.Descending) }, + OrderBy = new[] { (ItemSortBy.DateCreated, SortOrder.Descending) }, IsFolder = includeItemTypes.Length == 0 ? false : (bool?)null, ExcludeItemTypes = excludeItemTypes, IsVirtualItem = false, diff --git a/Emby.Server.Implementations/LiveTv/EmbyTV/EmbyTV.cs b/Emby.Server.Implementations/LiveTv/EmbyTV/EmbyTV.cs index ef5928e480..3b6bfce6a7 100644 --- a/Emby.Server.Implementations/LiveTv/EmbyTV/EmbyTV.cs +++ b/Emby.Server.Implementations/LiveTv/EmbyTV/EmbyTV.cs @@ -1582,15 +1582,15 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV return; } - var episodesToDelete = (librarySeries.GetItemList(new InternalItemsQuery + var episodesToDelete = librarySeries.GetItemList(new InternalItemsQuery { - OrderBy = new[] { new ValueTuple(ItemSortBy.DateCreated, SortOrder.Descending) }, + OrderBy = new[] { (ItemSortBy.DateCreated, SortOrder.Descending) }, IsVirtualItem = false, IsFolder = false, Recursive = true, DtoOptions = new DtoOptions(true) - })) + }) .Where(i => i.IsFileProtocol && File.Exists(i.Path)) .Skip(seriesTimer.KeepUpTo - 1) .ToList(); @@ -2260,7 +2260,7 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV }, MinStartDate = startDateUtc.AddMinutes(-3), MaxStartDate = startDateUtc.AddMinutes(3), - OrderBy = new[] { new ValueTuple(ItemSortBy.StartDate, SortOrder.Ascending) } + OrderBy = new[] { (ItemSortBy.StartDate, SortOrder.Ascending) } }; if (!string.IsNullOrWhiteSpace(channelId)) diff --git a/Emby.Server.Implementations/LiveTv/LiveTvManager.cs b/Emby.Server.Implementations/LiveTv/LiveTvManager.cs index d4bd598e38..2ecf4e1847 100644 --- a/Emby.Server.Implementations/LiveTv/LiveTvManager.cs +++ b/Emby.Server.Implementations/LiveTv/LiveTvManager.cs @@ -209,16 +209,16 @@ namespace Emby.Server.Implementations.LiveTv var orderBy = internalQuery.OrderBy.ToList(); - orderBy.AddRange(query.SortBy.Select(i => new ValueTuple(i, query.SortOrder ?? SortOrder.Ascending))); + orderBy.AddRange(query.SortBy.Select(i => (i, query.SortOrder ?? SortOrder.Ascending))); if (query.EnableFavoriteSorting) { - orderBy.Insert(0, new ValueTuple(ItemSortBy.IsFavoriteOrLiked, SortOrder.Descending)); + orderBy.Insert(0, (ItemSortBy.IsFavoriteOrLiked, SortOrder.Descending)); } if (!internalQuery.OrderBy.Any(i => string.Equals(i.Item1, ItemSortBy.SortName, StringComparison.OrdinalIgnoreCase))) { - orderBy.Add(new ValueTuple(ItemSortBy.SortName, SortOrder.Ascending)); + orderBy.Add((ItemSortBy.SortName, SortOrder.Ascending)); } internalQuery.OrderBy = orderBy.ToArray(); @@ -772,22 +772,22 @@ namespace Emby.Server.Implementations.LiveTv var topFolder = GetInternalLiveTvFolder(cancellationToken); - if (query.OrderBy.Length == 0) + if (query.OrderBy.Count == 0) { if (query.IsAiring ?? false) { // Unless something else was specified, order by start date to take advantage of a specialized index - query.OrderBy = new ValueTuple[] + query.OrderBy = new[] { - new ValueTuple(ItemSortBy.StartDate, SortOrder.Ascending) + (ItemSortBy.StartDate, SortOrder.Ascending) }; } else { // Unless something else was specified, order by start date to take advantage of a specialized index - query.OrderBy = new ValueTuple[] + query.OrderBy = new[] { - new ValueTuple(ItemSortBy.StartDate, SortOrder.Ascending) + (ItemSortBy.StartDate, SortOrder.Ascending) }; } } @@ -871,7 +871,7 @@ namespace Emby.Server.Implementations.LiveTv IsSports = query.IsSports, IsKids = query.IsKids, EnableTotalRecordCount = query.EnableTotalRecordCount, - OrderBy = new[] { new ValueTuple(ItemSortBy.StartDate, SortOrder.Ascending) }, + OrderBy = new[] { (ItemSortBy.StartDate, SortOrder.Ascending) }, TopParentIds = new[] { topFolder.Id }, DtoOptions = options, GenreIds = query.GenreIds @@ -1396,7 +1396,7 @@ namespace Emby.Server.Implementations.LiveTv IsVirtualItem = false, Limit = limit, StartIndex = query.StartIndex, - OrderBy = new[] { new ValueTuple(ItemSortBy.DateCreated, SortOrder.Descending) }, + OrderBy = new[] { (ItemSortBy.DateCreated, SortOrder.Descending) }, EnableTotalRecordCount = query.EnableTotalRecordCount, IncludeItemTypes = includeItemTypes.ToArray(), ExcludeItemTypes = excludeItemTypes.ToArray(), @@ -1894,7 +1894,7 @@ namespace Emby.Server.Implementations.LiveTv MaxStartDate = now, MinEndDate = now, Limit = channelIds.Length, - OrderBy = new[] { new ValueTuple(ItemSortBy.StartDate, SortOrder.Ascending) }, + OrderBy = new[] { (ItemSortBy.StartDate, SortOrder.Ascending) }, TopParentIds = new[] { GetInternalLiveTvFolder(CancellationToken.None).Id }, DtoOptions = options diff --git a/Emby.Server.Implementations/Playlists/PlaylistImageProvider.cs b/Emby.Server.Implementations/Playlists/PlaylistImageProvider.cs index cad66a80fd..2dfe59088d 100644 --- a/Emby.Server.Implementations/Playlists/PlaylistImageProvider.cs +++ b/Emby.Server.Implementations/Playlists/PlaylistImageProvider.cs @@ -62,7 +62,6 @@ namespace Emby.Server.Implementations.Playlists return null; }) .Where(i => i != null) - .OrderBy(i => Guid.NewGuid()) .GroupBy(x => x.Id) .Select(x => x.First()) .ToList(); @@ -84,7 +83,7 @@ namespace Emby.Server.Implementations.Playlists { Genres = new[] { item.Name }, IncludeItemTypes = new[] { typeof(MusicAlbum).Name, typeof(MusicVideo).Name, typeof(Audio).Name }, - OrderBy = new[] { new ValueTuple(ItemSortBy.Random, SortOrder.Ascending) }, + OrderBy = new[] { (ItemSortBy.Random, SortOrder.Ascending) }, Limit = 4, Recursive = true, ImageTypes = new[] { ImageType.Primary }, @@ -108,7 +107,7 @@ namespace Emby.Server.Implementations.Playlists { Genres = new[] { item.Name }, IncludeItemTypes = new[] { typeof(Series).Name, typeof(Movie).Name }, - OrderBy = new[] { new ValueTuple(ItemSortBy.Random, SortOrder.Ascending) }, + OrderBy = new[] { (ItemSortBy.Random, SortOrder.Ascending) }, Limit = 4, Recursive = true, ImageTypes = new[] { ImageType.Primary }, diff --git a/Emby.Server.Implementations/Session/SessionManager.cs b/Emby.Server.Implementations/Session/SessionManager.cs index d1392e1623..b87ca3a114 100644 --- a/Emby.Server.Implementations/Session/SessionManager.cs +++ b/Emby.Server.Implementations/Session/SessionManager.cs @@ -1061,7 +1061,7 @@ namespace Emby.Server.Implementations.Session var session = GetSessionToRemoteControl(sessionId); - var user = !session.UserId.Equals(Guid.Empty) ? _userManager.GetUserById(session.UserId) : null; + var user = session.UserId == Guid.Empty ? null : _userManager.GetUserById(session.UserId); List items; @@ -1086,7 +1086,7 @@ namespace Emby.Server.Implementations.Session if (command.PlayCommand == PlayCommand.PlayShuffle) { - items = items.OrderBy(i => Guid.NewGuid()).ToList(); + items.Shuffle(); command.PlayCommand = PlayCommand.PlayNow; } @@ -1100,28 +1100,27 @@ namespace Emby.Server.Implementations.Session } } - if (user != null && command.ItemIds.Length == 1 && user.Configuration.EnableNextEpisodeAutoPlay) + if (user != null + && command.ItemIds.Length == 1 + && user.Configuration.EnableNextEpisodeAutoPlay + && _libraryManager.GetItemById(command.ItemIds[0]) is Episode episode) { - var episode = _libraryManager.GetItemById(command.ItemIds[0]) as Episode; - if (episode != null) + var series = episode.Series; + if (series != null) { - var series = episode.Series; - if (series != null) + var episodes = series.GetEpisodes( + user, + new DtoOptions(false) + { + EnableImages = false + }) + .Where(i => !i.IsVirtualItem) + .SkipWhile(i => i.Id != episode.Id) + .ToList(); + + if (episodes.Count > 0) { - var episodes = series.GetEpisodes( - user, - new DtoOptions(false) - { - EnableImages = false - }) - .Where(i => !i.IsVirtualItem) - .SkipWhile(i => i.Id != episode.Id) - .ToList(); - - if (episodes.Count > 0) - { - command.ItemIds = episodes.Select(i => i.Id).ToArray(); - } + command.ItemIds = episodes.Select(i => i.Id).ToArray(); } } } @@ -1146,7 +1145,7 @@ namespace Emby.Server.Implementations.Session if (item == null) { _logger.LogError("A non-existant item Id {0} was passed into TranslateItemForPlayback", id); - return new List(); + return Array.Empty(); } if (item is IItemByName byName) @@ -1164,7 +1163,7 @@ namespace Emby.Server.Implementations.Session } }, IsVirtualItem = false, - OrderBy = new ValueTuple[] { new ValueTuple(ItemSortBy.SortName, SortOrder.Ascending) } + OrderBy = new[] { (ItemSortBy.SortName, SortOrder.Ascending) } }); } @@ -1185,12 +1184,11 @@ namespace Emby.Server.Implementations.Session } }, IsVirtualItem = false, - OrderBy = new ValueTuple[] { new ValueTuple(ItemSortBy.SortName, SortOrder.Ascending) } - + OrderBy = new[] { (ItemSortBy.SortName, SortOrder.Ascending) } }); } - return new List { item }; + return new[] { item }; } private IEnumerable TranslateItemForInstantMix(Guid id, User user) diff --git a/Emby.Server.Implementations/UserViews/DynamicImageProvider.cs b/Emby.Server.Implementations/UserViews/DynamicImageProvider.cs index f485204436..78ac95f85e 100644 --- a/Emby.Server.Implementations/UserViews/DynamicImageProvider.cs +++ b/Emby.Server.Implementations/UserViews/DynamicImageProvider.cs @@ -86,20 +86,17 @@ namespace Emby.Server.Implementations.UserViews { return items .Where(i => i.HasImage(ImageType.Primary) || i.HasImage(ImageType.Thumb)) - .OrderBy(i => Guid.NewGuid()) .ToList(); } return items .Where(i => i.HasImage(ImageType.Primary)) - .OrderBy(i => Guid.NewGuid()) .ToList(); } protected override bool Supports(BaseItem item) { - var view = item as UserView; - if (view != null) + if (item is UserView view) { return IsUsingCollectionStrip(view); } diff --git a/MediaBrowser.Api/Movies/MoviesService.cs b/MediaBrowser.Api/Movies/MoviesService.cs index 7abedd8ef6..889ebc9281 100644 --- a/MediaBrowser.Api/Movies/MoviesService.cs +++ b/MediaBrowser.Api/Movies/MoviesService.cs @@ -198,12 +198,10 @@ namespace MediaBrowser.Api.Movies var mostRecentMovies = recentlyPlayedMovies.Take(6).ToList(); // Get recently played directors var recentDirectors = GetDirectors(mostRecentMovies) - .OrderBy(i => Guid.NewGuid()) .ToList(); // Get recently played actors var recentActors = GetActors(mostRecentMovies) - .OrderBy(i => Guid.NewGuid()) .ToList(); var similarToRecentlyPlayed = GetSimilarTo(user, recentlyPlayedMovies, itemLimit, dtoOptions, RecommendationType.SimilarToRecentlyPlayed).GetEnumerator(); diff --git a/MediaBrowser.Api/TvShowsService.cs b/MediaBrowser.Api/TvShowsService.cs index ac2eca733d..b843f7096f 100644 --- a/MediaBrowser.Api/TvShowsService.cs +++ b/MediaBrowser.Api/TvShowsService.cs @@ -485,7 +485,7 @@ namespace MediaBrowser.Api if (string.Equals(request.SortBy, ItemSortBy.Random, StringComparison.OrdinalIgnoreCase)) { - episodes = episodes.OrderBy(i => Guid.NewGuid()).ToList(); + episodes.Shuffle(); } var returnItems = episodes; diff --git a/MediaBrowser.Api/UserLibrary/ItemsService.cs b/MediaBrowser.Api/UserLibrary/ItemsService.cs index 1511420d3d..81142b7289 100644 --- a/MediaBrowser.Api/UserLibrary/ItemsService.cs +++ b/MediaBrowser.Api/UserLibrary/ItemsService.cs @@ -468,7 +468,7 @@ namespace MediaBrowser.Api.UserLibrary } // Apply default sorting if none requested - if (query.OrderBy.Length == 0) + if (query.OrderBy.Count == 0) { // Albums by artist if (query.ArtistIds.Length > 0 && query.IncludeItemTypes.Length == 1 && string.Equals(query.IncludeItemTypes[0], "MusicAlbum", StringComparison.OrdinalIgnoreCase)) diff --git a/MediaBrowser.Common/Extensions/ShuffleExtensions.cs b/MediaBrowser.Common/Extensions/ShuffleExtensions.cs new file mode 100644 index 0000000000..5889d09c4b --- /dev/null +++ b/MediaBrowser.Common/Extensions/ShuffleExtensions.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; + +namespace MediaBrowser.Common.Extensions +{ + /// + /// Provides Shuffle extensions methods for . + /// + public static class ShuffleExtensions + { + private static readonly Random _rng = new Random(); + + /// + /// Shuffles the items in a list. + /// + /// The list that should get shuffled. + /// The type. + public static void Shuffle(this IList list) + { + int n = list.Count; + while (n > 1) + { + n--; + int k = _rng.Next(n + 1); + T value = list[k]; + list[k] = list[n]; + list[n] = value; + } + } + } +} diff --git a/MediaBrowser.Controller/Entities/InternalItemsQuery.cs b/MediaBrowser.Controller/Entities/InternalItemsQuery.cs index 78f8590696..bd96059e32 100644 --- a/MediaBrowser.Controller/Entities/InternalItemsQuery.cs +++ b/MediaBrowser.Controller/Entities/InternalItemsQuery.cs @@ -155,7 +155,7 @@ namespace MediaBrowser.Controller.Entities public bool EnableGroupByMetadataKey { get; set; } public bool? HasChapterImages { get; set; } - public ValueTuple[] OrderBy { get; set; } + public IReadOnlyList<(string, SortOrder)> OrderBy { get; set; } public DateTime? MinDateCreated { get; set; } public DateTime? MinDateLastSaved { get; set; } diff --git a/MediaBrowser.Controller/Entities/TV/Series.cs b/MediaBrowser.Controller/Entities/TV/Series.cs index 76cb9a6514..2475b2b7ec 100644 --- a/MediaBrowser.Controller/Entities/TV/Series.cs +++ b/MediaBrowser.Controller/Entities/TV/Series.cs @@ -226,14 +226,16 @@ namespace MediaBrowser.Controller.Entities.TV query.AncestorWithPresentationUniqueKey = null; query.SeriesPresentationUniqueKey = seriesKey; - if (query.OrderBy.Length == 0) + if (query.OrderBy.Count == 0) { query.OrderBy = new[] { ItemSortBy.SortName }.Select(i => new ValueTuple(i, SortOrder.Ascending)).ToArray(); } + if (query.IncludeItemTypes.Length == 0) { query.IncludeItemTypes = new[] { typeof(Episode).Name, typeof(Season).Name }; } + query.IsVirtualItem = false; return LibraryManager.GetItemsResult(query); } diff --git a/MediaBrowser.Controller/Entities/UserViewBuilder.cs b/MediaBrowser.Controller/Entities/UserViewBuilder.cs index 454bdc4ae2..435a1e8dae 100644 --- a/MediaBrowser.Controller/Entities/UserViewBuilder.cs +++ b/MediaBrowser.Controller/Entities/UserViewBuilder.cs @@ -450,14 +450,16 @@ namespace MediaBrowser.Controller.Entities return SortAndPage(items, totalRecordLimit, query, libraryManager, true); } - public static QueryResult SortAndPage(IEnumerable items, + public static QueryResult SortAndPage( + IEnumerable items, int? totalRecordLimit, InternalItemsQuery query, - ILibraryManager libraryManager, bool enableSorting) + ILibraryManager libraryManager, + bool enableSorting) { if (enableSorting) { - if (query.OrderBy.Length > 0) + if (query.OrderBy.Count > 0) { items = libraryManager.Sort(items, query.User, query.OrderBy); } diff --git a/MediaBrowser.Controller/Playlists/Playlist.cs b/MediaBrowser.Controller/Playlists/Playlist.cs index b45043f92a..3b08e72b92 100644 --- a/MediaBrowser.Controller/Playlists/Playlist.cs +++ b/MediaBrowser.Controller/Playlists/Playlist.cs @@ -181,7 +181,7 @@ namespace MediaBrowser.Controller.Playlists { Recursive = true, IsFolder = false, - OrderBy = new[] { ItemSortBy.SortName }.Select(i => new ValueTuple(i, SortOrder.Ascending)).ToArray(), + OrderBy = new[] { (ItemSortBy.SortName, SortOrder.Ascending) }, MediaTypes = new[] { mediaType }, EnableTotalRecordCount = false, DtoOptions = options