Files
CliFx/CliFx/Utils/Extensions/CollectionExtensions.cs
2022-01-04 22:31:50 +02:00

40 lines
1.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace CliFx.Utils.Extensions;
internal static class CollectionExtensions
{
public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> source)
where T : class
{
foreach (var i in source)
{
if (i is not null)
yield return i;
}
}
public static IEnumerable<string> WhereNotNullOrWhiteSpace(this IEnumerable<string?> source)
{
foreach (var i in source)
{
if (!string.IsNullOrWhiteSpace(i))
yield return i;
}
}
public static void RemoveRange<T>(this ICollection<T> source, IEnumerable<T> items)
{
foreach (var item in items)
source.Remove(item);
}
public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(
this IDictionary dictionary,
IEqualityComparer<TKey> comparer) =>
dictionary
.Cast<DictionaryEntry>()
.ToDictionary(entry => (TKey) entry.Key, entry => (TValue) entry.Value, comparer);
}