72 lines
1.8 KiB
C#
72 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace BS.Shared.Extensions
|
|
{
|
|
public static class ICollectionExtensions
|
|
{
|
|
public static void AddIfNotIn<T>(this ICollection<T> pCollection, T pToAdd)
|
|
{
|
|
if (!pCollection.Contains(pToAdd))
|
|
{
|
|
pCollection.Add(pToAdd);
|
|
}
|
|
}
|
|
|
|
public static void AddRange<T>(this ICollection<T> pCollection, IEnumerable<T> pToAdd)
|
|
{
|
|
foreach (var iItem in pToAdd)
|
|
{
|
|
pCollection.Add(iItem);
|
|
}
|
|
}
|
|
|
|
public static bool Exists<T>(this IEnumerable<T> pCollection, Predicate<T> pMatch)
|
|
{
|
|
return pCollection.Any(iItem => pMatch(iItem));
|
|
}
|
|
|
|
public static void RemoveRange<T>(this ICollection<T> pList, IEnumerable<T> pToRemove)
|
|
{
|
|
foreach (T iItem in pToRemove.ToList())
|
|
{
|
|
pList.Remove(iItem);
|
|
}
|
|
}
|
|
|
|
public static void RemoveRange<T>(this ICollection<T> pList, Predicate<T> pMatch)
|
|
{
|
|
var lToDelete = pList.Where(t => pMatch(t)).ToHashSetEx();
|
|
pList.RemoveRange(lToDelete);
|
|
}
|
|
|
|
public static bool AreEqual<T>(this ICollection<T> pCollection, ICollection<T> pCollection2)
|
|
{
|
|
if(pCollection.Count != pCollection2.Count)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var tmp1 = pCollection.OrderBy(x => x.GetHashCode()).ToList();
|
|
var tmp2 = pCollection2.OrderBy(x => x.GetHashCode()).ToList();
|
|
|
|
return !pCollection.Where((t, i) => !tmp1.ElementAt(i).Equals(tmp2.ElementAt(i))).Any();
|
|
}
|
|
|
|
public static bool ListEquals<T>(ICollection<T> list1, ICollection<T> list2)
|
|
{
|
|
if(list1 is null && list2 is null)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if(list1 is null || list2 is null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return list1.Count == list2.Count && list1.All(list2.Contains);
|
|
}
|
|
}
|
|
} |