Use ToList() instead of ToArray() on sequences of unknown size

This commit is contained in:
Mark Monteiro 2020-03-03 18:18:00 +01:00
parent 6438771212
commit 3cb98fba55

View File

@ -196,7 +196,7 @@ namespace Emby.Server.Implementations.Playlists
// Retrieve all the items to be added to the playlist // Retrieve all the items to be added to the playlist
var items = GetPlaylistItems(itemIds, playlist.MediaType, user, options) var items = GetPlaylistItems(itemIds, playlist.MediaType, user, options)
.Where(i => i.SupportsAddingToPlaylist) .Where(i => i.SupportsAddingToPlaylist)
.ToArray(); .ToList();
// Remove duplicates from the new items to be added // Remove duplicates from the new items to be added
var existingIds = playlist.LinkedChildren.Select(c => c.ItemId).ToHashSet(); var existingIds = playlist.LinkedChildren.Select(c => c.ItemId).ToHashSet();
@ -205,23 +205,23 @@ namespace Emby.Server.Implementations.Playlists
.GroupBy(i => i.Id) .GroupBy(i => i.Id)
.Select(group => group.First()) .Select(group => group.First())
.Select(i => LinkedChild.Create(i)) .Select(i => LinkedChild.Create(i))
.ToArray(); .ToList();
// Log duplicates that have been ignored, if any // Log duplicates that have been ignored, if any
int numDuplicates = items.Length - uniqueItems.Length; int numDuplicates = items.Count - uniqueItems.Count;
if (numDuplicates > 0) if (numDuplicates > 0)
{ {
_logger.LogWarning("Ignored adding {DuplicateCount} duplicate items to playlist {PlaylistName}.", numDuplicates, playlist.Name); _logger.LogWarning("Ignored adding {DuplicateCount} duplicate items to playlist {PlaylistName}.", numDuplicates, playlist.Name);
} }
// Do nothing else if there are no items to add to the playlist // Do nothing else if there are no items to add to the playlist
if (uniqueItems.Length == 0) if (uniqueItems.Count == 0)
{ {
return; return;
} }
// Create a new array with the updated playlist items // Create a new array with the updated playlist items
var newLinkedChildren = new LinkedChild[playlist.LinkedChildren.Length + uniqueItems.Length]; var newLinkedChildren = new LinkedChild[playlist.LinkedChildren.Length + uniqueItems.Count];
playlist.LinkedChildren.CopyTo(newLinkedChildren, 0); playlist.LinkedChildren.CopyTo(newLinkedChildren, 0);
uniqueItems.CopyTo(newLinkedChildren, playlist.LinkedChildren.Length); uniqueItems.CopyTo(newLinkedChildren, playlist.LinkedChildren.Length);