using System; using System.Collections.Generic; using System.Linq; namespace BS.Shared.Extensions { public static class ICollectionExtensions { public static void AddIfNotIn(this ICollection pCollection, T pToAdd) { if (!pCollection.Contains(pToAdd)) { pCollection.Add(pToAdd); } } public static void AddRange(this ICollection pCollection, IEnumerable pToAdd) { foreach (var iItem in pToAdd) { pCollection.Add(iItem); } } public static bool Exists(this IEnumerable pCollection, Predicate pMatch) { return pCollection.Any(iItem => pMatch(iItem)); } public static void RemoveRange(this ICollection pList, IEnumerable pToRemove) { foreach (T iItem in pToRemove.ToList()) { pList.Remove(iItem); } } public static void RemoveRange(this ICollection pList, Predicate pMatch) { var lToDelete = pList.Where(t => pMatch(t)).ToHashSet(); pList.RemoveRange(lToDelete); } public static bool AreEqual(this ICollection pCollection, ICollection 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(); } } }