diff --git a/Jellyfin.Server.Implementations/Item/BaseItemRepository.cs b/Jellyfin.Server.Implementations/Item/BaseItemRepository.cs index e05f4c45dc..2c0ec69941 100644 --- a/Jellyfin.Server.Implementations/Item/BaseItemRepository.cs +++ b/Jellyfin.Server.Implementations/Item/BaseItemRepository.cs @@ -375,30 +375,29 @@ public sealed class BaseItemRepository // Find the top N group keys ordered by most recent DateCreated. // Movies group by PresentationUniqueKey (alternate versions like 4K/1080p share a key). // Music groups by Album. - List topGroupKeys; + Expression> groupKeyFilter; + Expression> groupKeySelector; + if (collectionType is CollectionType.movies) { - topGroupKeys = baseQuery - .Where(e => e.PresentationUniqueKey != null) - .GroupBy(e => e.PresentationUniqueKey) - .Select(g => new { GroupKey = g.Key!, MaxDate = g.Max(e => e.DateCreated) }) - .OrderByDescending(g => g.MaxDate) - .Take(limit) - .Select(g => g.GroupKey) - .ToList(); + groupKeyFilter = e => e.PresentationUniqueKey != null; + groupKeySelector = e => e.PresentationUniqueKey; } else { - topGroupKeys = baseQuery - .Where(e => e.Album != null) - .GroupBy(e => e.Album) - .Select(g => new { GroupKey = g.Key!, MaxDate = g.Max(e => e.DateCreated) }) - .OrderByDescending(g => g.MaxDate) - .Take(limit) - .Select(g => g.GroupKey) - .ToList(); + groupKeyFilter = e => e.Album != null; + groupKeySelector = e => e.Album; } + var topGroupKeys = baseQuery + .Where(groupKeyFilter) + .GroupBy(groupKeySelector) + .Select(g => new { GroupKey = g.Key!, MaxDate = g.Max(e => e.DateCreated) }) + .OrderByDescending(g => g.MaxDate) + .Take(limit) + .Select(g => g.GroupKey) + .ToList(); + // Get only the first (most recent) item ID per group using a lightweight projection, // then fetch full entities only for those items. This avoids loading all versions/tracks // with expensive navigation properties just to discard duplicates. @@ -1422,13 +1421,13 @@ public sealed class BaseItemRepository .ToArray(); var lookup = _itemTypeLookup.BaseItemKindNames; - var result = new ItemCounts - { - ItemCount = counts.Sum(c => c.Count) - }; + var result = new ItemCounts(); + var totalCount = 0; foreach (var count in counts) { + totalCount += count.Count; + if (string.Equals(count.Key, lookup[BaseItemKind.MusicAlbum], StringComparison.Ordinal)) { result.AlbumCount = count.Count; @@ -1475,6 +1474,8 @@ public sealed class BaseItemRepository } } + result.ItemCount = totalCount; + return result; }