The Free Software Media System - Server Backend & API
avoid Take(0) when limit == 0 (#14608)
evan3141598b2a8b94b636
diff --git a/Emby.Server.Implementations/Library/SearchEngine.cs b/Emby.Server.Implementations/Library/SearchEngine.csindex 9d81b835ce..c682118597 100644--- a/Emby.Server.Implementations/Library/SearchEngine.cs+++ b/Emby.Server.Implementations/Library/SearchEngine.cs@@ -42,7 +42,7 @@ namespace Emby.Server.Implementations.Libraryresults = results.GetRange(query.StartIndex.Value, totalRecordCount - query.StartIndex.Value);}- if (query.Limit.HasValue)+ if (query.Limit.HasValue && query.Limit.Value > 0){results = results.GetRange(0, Math.Min(query.Limit.Value, results.Count));}diff --git a/Emby.Server.Implementations/TV/TVSeriesManager.cs b/Emby.Server.Implementations/TV/TVSeriesManager.csindex ee2e18f735..cd98dbe86e 100644--- a/Emby.Server.Implementations/TV/TVSeriesManager.cs+++ b/Emby.Server.Implementations/TV/TVSeriesManager.cs@@ -266,7 +266,7 @@ namespace Emby.Server.Implementations.TVitems = items.Skip(query.StartIndex.Value);}- if (query.Limit.HasValue)+ if (query.Limit.HasValue && query.Limit.Value > 0){items = items.Take(query.Limit.Value);}diff --git a/Jellyfin.Server.Implementations/Devices/DeviceManager.cs b/Jellyfin.Server.Implementations/Devices/DeviceManager.csindex 51a1186452..bcf348f8c6 100644--- a/Jellyfin.Server.Implementations/Devices/DeviceManager.cs+++ b/Jellyfin.Server.Implementations/Devices/DeviceManager.cs@@ -158,7 +158,7 @@ namespace Jellyfin.Server.Implementations.Devicesdevices = devices.Skip(query.Skip.Value);}- if (query.Limit.HasValue)+ if (query.Limit.HasValue && query.Limit.Value > 0){devices = devices.Take(query.Limit.Value);}diff --git a/Jellyfin.Server.Implementations/Item/BaseItemRepository.cs b/Jellyfin.Server.Implementations/Item/BaseItemRepository.csindex 57d874e59f..dfe46ef8f6 100644--- a/Jellyfin.Server.Implementations/Item/BaseItemRepository.cs+++ b/Jellyfin.Server.Implementations/Item/BaseItemRepository.cs@@ -250,7 +250,7 @@ public sealed class BaseItemRepositorypublic QueryResult<BaseItemDto> GetItems(InternalItemsQuery filter){ArgumentNullException.ThrowIfNull(filter);- if (!filter.EnableTotalRecordCount || (!filter.Limit.HasValue && (filter.StartIndex ?? 0) == 0))+ if (!filter.EnableTotalRecordCount || ((filter.Limit ?? 0) == 0 && (filter.StartIndex ?? 0) == 0)){var returnList = GetItemList(filter);return new QueryResult<BaseItemDto>(@@ -326,7 +326,7 @@ public sealed class BaseItemRepository.OrderByDescending(g => g.MaxDateCreated).Select(g => g);- if (filter.Limit.HasValue)+ if (filter.Limit.HasValue && filter.Limit.Value > 0){subqueryGrouped = subqueryGrouped.Take(filter.Limit.Value);}@@ -367,7 +367,7 @@ public sealed class BaseItemRepository.OrderByDescending(g => g.LastPlayedDate).Select(g => g.Key!);- if (filter.Limit.HasValue)+ if (filter.Limit.HasValue && filter.Limit.Value > 0){query = query.Take(filter.Limit.Value);}@@ -425,19 +425,14 @@ public sealed class BaseItemRepositoryprivate IQueryable<BaseItemEntity> ApplyQueryPaging(IQueryable<BaseItemEntity> dbQuery, InternalItemsQuery filter){- if (filter.Limit.HasValue || filter.StartIndex.HasValue)+ if (filter.StartIndex.HasValue && filter.StartIndex.Value > 0){- var offset = filter.StartIndex ?? 0;-- if (offset > 0)- {- dbQuery = dbQuery.Skip(offset);- }+ dbQuery = dbQuery.Skip(filter.StartIndex.Value);+ }- if (filter.Limit.HasValue)- {- dbQuery = dbQuery.Take(filter.Limit.Value);- }+ if (filter.Limit.HasValue && filter.Limit.Value > 0)+ {+ dbQuery = dbQuery.Take(filter.Limit.Value);}return dbQuery;@@ -1190,7 +1185,7 @@ public sealed class BaseItemRepository{ArgumentNullException.ThrowIfNull(filter);- if (!filter.Limit.HasValue)+ if (!(filter.Limit.HasValue && filter.Limit.Value > 0)){filter.EnableTotalRecordCount = false;}@@ -1269,19 +1264,14 @@ public sealed class BaseItemRepositoryresult.TotalRecordCount = query.Count();}- if (filter.Limit.HasValue || filter.StartIndex.HasValue)+ if (filter.StartIndex.HasValue && filter.StartIndex.Value > 0){- var offset = filter.StartIndex ?? 0;-- if (offset > 0)- {- query = query.Skip(offset);- }+ query = query.Skip(filter.StartIndex.Value);+ }- if (filter.Limit.HasValue)- {- query = query.Take(filter.Limit.Value);- }+ if (filter.Limit.HasValue && filter.Limit.Value > 0)+ {+ query = query.Take(filter.Limit.Value);}IQueryable<BaseItemEntity>? itemCountQuery = null;@@ -1362,7 +1352,7 @@ public sealed class BaseItemRepositoryprivate static void PrepareFilterQuery(InternalItemsQuery query){- if (query.Limit.HasValue && query.EnableGroupByMetadataKey)+ if (query.Limit.HasValue && query.Limit.Value > 0 && query.EnableGroupByMetadataKey){query.Limit = query.Limit.Value + 4;}diff --git a/MediaBrowser.Controller/Entities/UserViewBuilder.cs b/MediaBrowser.Controller/Entities/UserViewBuilder.csindex 4f9e9261b6..bed7554b19 100644--- a/MediaBrowser.Controller/Entities/UserViewBuilder.cs+++ b/MediaBrowser.Controller/Entities/UserViewBuilder.cs@@ -455,7 +455,7 @@ namespace MediaBrowser.Controller.Entitiesvar itemsArray = totalRecordLimit.HasValue ? items.Take(totalRecordLimit.Value).ToArray() : items.ToArray();var totalCount = itemsArray.Length;- if (query.Limit.HasValue)+ if (query.Limit.HasValue && query.Limit.Value > 0){itemsArray = itemsArray.Skip(query.StartIndex ?? 0).Take(query.Limit.Value).ToArray();}diff --git a/src/Jellyfin.LiveTv/Channels/ChannelManager.cs b/src/Jellyfin.LiveTv/Channels/ChannelManager.csindex 8ee129a575..2b8e5a0a08 100644--- a/src/Jellyfin.LiveTv/Channels/ChannelManager.cs+++ b/src/Jellyfin.LiveTv/Channels/ChannelManager.cs@@ -240,12 +240,9 @@ namespace Jellyfin.LiveTv.Channelsvar all = channels;var totalCount = all.Count;- if (query.StartIndex.HasValue || query.Limit.HasValue)- {- int startIndex = query.StartIndex ?? 0;- int count = query.Limit is null ? totalCount - startIndex : Math.Min(query.Limit.Value, totalCount - startIndex);- all = all.GetRange(startIndex, count);- }+ int startIndex = query.StartIndex ?? 0;+ int count = (query.Limit ?? 0) > 0 ? Math.Min(query.Limit.Value, totalCount - startIndex) : totalCount - startIndex;+ all = all.GetRange(query.StartIndex ?? 0, count);if (query.RefreshLatestChannelItems){diff --git a/src/Jellyfin.LiveTv/LiveTvManager.cs b/src/Jellyfin.LiveTv/LiveTvManager.csindex 53bc6751fc..1d18ade9dc 100644--- a/src/Jellyfin.LiveTv/LiveTvManager.cs+++ b/src/Jellyfin.LiveTv/LiveTvManager.cs@@ -287,7 +287,7 @@ namespace Jellyfin.LiveTvGenreIds = query.GenreIds};- if (query.Limit.HasValue)+ if (query.Limit.HasValue && query.Limit.Value > 0){internalQuery.Limit = Math.Max(query.Limit.Value * 4, 200);}@@ -305,7 +305,7 @@ namespace Jellyfin.LiveTvIEnumerable<BaseItem> programs = orderedPrograms;- if (query.Limit.HasValue)+ if (query.Limit.HasValue && query.Limit.Value > 0){programs = programs.Take(query.Limit.Value);}