Merge pull request #794 from Bond-009/lesslinq

Remove MoreLINQ
This commit is contained in:
Andrew Rabert 2019-02-02 14:19:54 -05:00 committed by GitHub
commit 20dac6d6b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 54 additions and 122 deletions

View File

@ -904,7 +904,7 @@ namespace Emby.Server.Implementations
PlaylistManager = new PlaylistManager(LibraryManager, FileSystemManager, LibraryMonitor, LoggerFactory, UserManager, ProviderManager);
RegisterSingleInstance(PlaylistManager);
LiveTvManager = new LiveTvManager(this, ServerConfigurationManager, LoggerFactory, ItemRepository, ImageProcessor, UserDataManager, DtoService, UserManager, LibraryManager, TaskManager, LocalizationManager, JsonSerializer, ProviderManager, FileSystemManager, () => ChannelManager);
LiveTvManager = new LiveTvManager(this, ServerConfigurationManager, LoggerFactory, ItemRepository, ImageProcessor, UserDataManager, DtoService, UserManager, LibraryManager, TaskManager, LocalizationManager, JsonSerializer, FileSystemManager, () => ChannelManager);
RegisterSingleInstance(LiveTvManager);
UserViewManager = new UserViewManager(LibraryManager, LocalizationManager, UserManager, ChannelManager, LiveTvManager, ServerConfigurationManager);

View File

@ -70,7 +70,8 @@ namespace Emby.Server.Implementations.Collections
return null;
})
.Where(i => i != null)
.DistinctBy(i => i.Id)
.GroupBy(x => x.Id)
.Select(x => x.First())
.OrderBy(i => Guid.NewGuid())
.ToList();
}

View File

@ -620,7 +620,8 @@ namespace Emby.Server.Implementations.Dto
}
}).Where(i => i != null)
.DistinctBy(i => i.Name, StringComparer.OrdinalIgnoreCase)
.GroupBy(i => i.Name, StringComparer.OrdinalIgnoreCase)
.Select(x => x.First())
.ToDictionary(i => i.Name, StringComparer.OrdinalIgnoreCase);
for (var i = 0; i < people.Count; i++)

View File

@ -277,13 +277,20 @@ namespace Emby.Server.Implementations.EntryPoints
lock (_libraryChangedSyncLock)
{
// Remove dupes in case some were saved multiple times
var foldersAddedTo = _foldersAddedTo.DistinctBy(i => i.Id).ToList();
var foldersAddedTo = _foldersAddedTo
.GroupBy(x => x.Id)
.Select(x => x.First())
.ToList();
var foldersRemovedFrom = _foldersRemovedFrom.DistinctBy(i => i.Id).ToList();
var foldersRemovedFrom = _foldersRemovedFrom
.GroupBy(x => x.Id)
.Select(x => x.First())
.ToList();
var itemsUpdated = _itemsUpdated
.Where(i => !_itemsAdded.Contains(i))
.DistinctBy(i => i.Id)
.GroupBy(x => x.Id)
.Select(x => x.First())
.ToList();
SendChangeNotifications(_itemsAdded.ToList(), itemsUpdated, _itemsRemoved.ToList(), foldersAddedTo, foldersRemovedFrom, CancellationToken.None);

View File

@ -121,7 +121,8 @@ namespace Emby.Server.Implementations.EntryPoints
var user = _userManager.GetUserById(userId);
var dtoList = changedItems
.DistinctBy(i => i.Id)
.GroupBy(x => x.Id)
.Select(x => x.First())
.Select(i =>
{
var dto = _userDataManager.GetUserDataDto(i, user);

View File

@ -146,8 +146,8 @@ namespace Emby.Server.Implementations.IO
.Distinct(StringComparer.OrdinalIgnoreCase)
.Select(GetAffectedBaseItem)
.Where(item => item != null)
.DistinctBy(i => i.Id)
.ToList();
.GroupBy(x => x.Id)
.Select(x => x.First());
foreach (var item in itemsToRefresh)
{

View File

@ -6,7 +6,6 @@ using System.Threading.Tasks;
using Emby.Server.Implementations.Library;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Net;
using MediaBrowser.Common.Progress;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Channels;
@ -24,7 +23,6 @@ using MediaBrowser.Controller.Sorting;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Events;
using MediaBrowser.Model.Extensions;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.LiveTv;
@ -48,7 +46,6 @@ namespace Emby.Server.Implementations.LiveTv
private readonly ILibraryManager _libraryManager;
private readonly ITaskManager _taskManager;
private readonly IJsonSerializer _jsonSerializer;
private readonly IProviderManager _providerManager;
private readonly Func<IChannelManager> _channelManager;
private readonly IDtoService _dtoService;
@ -85,7 +82,6 @@ namespace Emby.Server.Implementations.LiveTv
ITaskManager taskManager,
ILocalizationManager localization,
IJsonSerializer jsonSerializer,
IProviderManager providerManager,
IFileSystem fileSystem,
Func<IChannelManager> channelManager)
{
@ -97,7 +93,6 @@ namespace Emby.Server.Implementations.LiveTv
_taskManager = taskManager;
_localization = localization;
_jsonSerializer = jsonSerializer;
_providerManager = providerManager;
_fileSystem = fileSystem;
_dtoService = dtoService;
_userDataManager = userDataManager;
@ -2469,7 +2464,8 @@ namespace Emby.Server.Implementations.LiveTv
.Where(i => i != null)
.Where(i => i.IsVisibleStandalone(user))
.SelectMany(i => _libraryManager.GetCollectionFolders(i))
.DistinctBy(i => i.Id)
.GroupBy(x => x.Id)
.Select(x => x.First())
.OrderBy(i => i.SortName)
.ToList();

View File

@ -111,7 +111,8 @@ namespace Emby.Server.Implementations.Networking
.OrderBy(i => i.AddressFamily == AddressFamily.InterNetwork ? 0 : 1)
.ThenBy(i => listClone.IndexOf(i))
.Where(FilterIpAddress)
.DistinctBy(i => i.ToString())
.GroupBy(i => i.ToString())
.Select(x => x.First())
.ToList();
}
@ -429,7 +430,8 @@ namespace Emby.Server.Implementations.Networking
return new List<IPAddress>();
}
}).DistinctBy(i => i.ToString())
}).GroupBy(i => i.ToString())
.Select(x => x.First())
.ToList();
}

View File

@ -13,7 +13,6 @@ using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Playlists;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Extensions;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Querying;
@ -64,7 +63,8 @@ namespace Emby.Server.Implementations.Playlists
})
.Where(i => i != null)
.OrderBy(i => Guid.NewGuid())
.DistinctBy(i => i.Id)
.GroupBy(x => x.Id)
.Select(x => x.First())
.ToList();
}
}

View File

@ -12,7 +12,6 @@ using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Extensions;
using MediaBrowser.Model.IO;
namespace Emby.Server.Implementations.UserViews
@ -83,7 +82,8 @@ namespace Emby.Server.Implementations.UserViews
return i;
}).DistinctBy(i => i.Id);
}).GroupBy(x => x.Id)
.Select(x => x.First());
if (isUsingCollectionStrip)
{

View File

@ -551,7 +551,8 @@ namespace MediaBrowser.Api.Library
Name = i.Name,
DefaultEnabled = IsSaverEnabledByDefault(i.Name, types, isNewLibrary)
})
.DistinctBy(i => i.Name, StringComparer.OrdinalIgnoreCase)
.GroupBy(i => i.Name, StringComparer.OrdinalIgnoreCase)
.Select(x => x.First())
.ToArray();
result.MetadataReaders = plugins
@ -561,7 +562,8 @@ namespace MediaBrowser.Api.Library
Name = i.Name,
DefaultEnabled = true
})
.DistinctBy(i => i.Name, StringComparer.OrdinalIgnoreCase)
.GroupBy(i => i.Name, StringComparer.OrdinalIgnoreCase)
.Select(x => x.First())
.ToArray();
result.SubtitleFetchers = plugins
@ -571,7 +573,8 @@ namespace MediaBrowser.Api.Library
Name = i.Name,
DefaultEnabled = true
})
.DistinctBy(i => i.Name, StringComparer.OrdinalIgnoreCase)
.GroupBy(i => i.Name, StringComparer.OrdinalIgnoreCase)
.Select(x => x.First())
.ToArray();
var typeOptions = new List<LibraryTypeOptions>();
@ -593,7 +596,8 @@ namespace MediaBrowser.Api.Library
Name = i.Name,
DefaultEnabled = IsMetadataFetcherEnabledByDefault(i.Name, type, isNewLibrary)
})
.DistinctBy(i => i.Name, StringComparer.OrdinalIgnoreCase)
.GroupBy(i => i.Name, StringComparer.OrdinalIgnoreCase)
.Select(x => x.First())
.ToArray(),
ImageFetchers = plugins
@ -604,7 +608,8 @@ namespace MediaBrowser.Api.Library
Name = i.Name,
DefaultEnabled = IsImageFetcherEnabledByDefault(i.Name, type, isNewLibrary)
})
.DistinctBy(i => i.Name, StringComparer.OrdinalIgnoreCase)
.GroupBy(i => i.Name, StringComparer.OrdinalIgnoreCase)
.Select(x => x.First())
.ToArray(),
SupportedImageTypes = plugins

View File

@ -268,7 +268,8 @@ namespace MediaBrowser.Api.Movies
EnableGroupByMetadataKey = true,
DtoOptions = dtoOptions
}).DistinctBy(i => i.GetProviderId(MetadataProviders.Imdb) ?? Guid.NewGuid().ToString("N"))
}).GroupBy(i => i.GetProviderId(MetadataProviders.Imdb) ?? Guid.NewGuid().ToString("N"))
.Select(x => x.First())
.Take(itemLimit)
.ToList();
@ -308,7 +309,8 @@ namespace MediaBrowser.Api.Movies
EnableGroupByMetadataKey = true,
DtoOptions = dtoOptions
}).DistinctBy(i => i.GetProviderId(MetadataProviders.Imdb) ?? Guid.NewGuid().ToString("N"))
}).GroupBy(i => i.GetProviderId(MetadataProviders.Imdb) ?? Guid.NewGuid().ToString("N"))
.Select(x => x.First())
.Take(itemLimit)
.ToList();

View File

@ -335,7 +335,11 @@ namespace MediaBrowser.Controller.Entities
.OfType<Folder>()
.ToList();
return PhysicalLocations.Where(i => !FileSystem.AreEqual(i, Path)).SelectMany(i => GetPhysicalParents(i, rootChildren)).DistinctBy(i => i.Id);
return PhysicalLocations
.Where(i => !FileSystem.AreEqual(i, Path))
.SelectMany(i => GetPhysicalParents(i, rootChildren))
.GroupBy(x => x.Id)
.Select(x => x.First());
}
private IEnumerable<Folder> GetPhysicalParents(string path, List<Folder> rootChildren)

View File

@ -270,7 +270,7 @@ namespace MediaBrowser.Controller.Entities.TV
// This depends on settings for that series
// When this happens, remove the duplicate from season 0
return allEpisodes.DistinctBy(i => i.Id).Reverse();
return allEpisodes.GroupBy(i => i.Id).Select(x => x.First()).Reverse();
}
public async Task RefreshAllMetadata(MetadataRefreshOptions refreshOptions, IProgress<double> progress, CancellationToken cancellationToken)

View File

@ -1,7 +1,7 @@
using System;
using System.Linq;
using System.Collections.Generic;
using MediaBrowser.Controller.Extensions;
using MediaBrowser.Model.Extensions;
namespace MediaBrowser.Controller.Library
{
@ -14,13 +14,11 @@ namespace MediaBrowser.Controller.Library
return string.Empty;
}
//return name;
return name.RemoveDiacritics();
}
public static IEnumerable<string> DistinctNames(this IEnumerable<string> names)
{
return names.DistinctBy(RemoveDiacritics, StringComparer.OrdinalIgnoreCase);
}
=> names.GroupBy(RemoveDiacritics, StringComparer.OrdinalIgnoreCase)
.Select(x => x.First());
}
}

View File

@ -1,85 +0,0 @@
using System;
using System.Collections.Generic;
// TODO: @bond Remove
namespace MediaBrowser.Model.Extensions
{
// MoreLINQ - Extensions to LINQ to Objects
// Copyright (c) 2008 Jonathan Skeet. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
public static class LinqExtensions
{
/// <summary>
/// Returns all distinct elements of the given source, where "distinctness"
/// is determined via a projection and the default equality comparer for the projected type.
/// </summary>
/// <remarks>
/// This operator uses deferred execution and streams the results, although
/// a set of already-seen keys is retained. If a key is seen multiple times,
/// only the first element with that key is returned.
/// </remarks>
/// <typeparam name="TSource">Type of the source sequence</typeparam>
/// <typeparam name="TKey">Type of the projected element</typeparam>
/// <param name="source">Source sequence</param>
/// <param name="keySelector">Projection for determining "distinctness"</param>
/// <returns>A sequence consisting of distinct elements from the source sequence,
/// comparing them by the specified key projection.</returns>
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector)
{
return source.DistinctBy(keySelector, null);
}
/// <summary>
/// Returns all distinct elements of the given source, where "distinctness"
/// is determined via a projection and the specified comparer for the projected type.
/// </summary>
/// <remarks>
/// This operator uses deferred execution and streams the results, although
/// a set of already-seen keys is retained. If a key is seen multiple times,
/// only the first element with that key is returned.
/// </remarks>
/// <typeparam name="TSource">Type of the source sequence</typeparam>
/// <typeparam name="TKey">Type of the projected element</typeparam>
/// <param name="source">Source sequence</param>
/// <param name="keySelector">Projection for determining "distinctness"</param>
/// <param name="comparer">The equality comparer to use to determine whether or not keys are equal.
/// If null, the default equality comparer for <c>TSource</c> is used.</param>
/// <returns>A sequence consisting of distinct elements from the source sequence,
/// comparing them by the specified key projection.</returns>
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
return DistinctByImpl(source, keySelector, comparer);
}
private static IEnumerable<TSource> DistinctByImpl<TSource, TKey>(IEnumerable<TSource> source,
Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
{
var knownKeys = new HashSet<TKey>(comparer);
foreach (var element in source)
{
if (knownKeys.Add(keySelector(element)))
{
yield return element;
}
}
}
}
}