164 lines
4.2 KiB
C#
164 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace BS.Shared.Extensions
|
|
{
|
|
public static class IListTExtensions
|
|
{
|
|
public static void AddIfNotIn<T>(this IList<T> pList, T pToAdd)
|
|
{
|
|
if (!pList.Contains(pToAdd))
|
|
{
|
|
pList.Add(pToAdd);
|
|
}
|
|
}
|
|
|
|
public static void AddRangeIfElementsNotIn<T>(this IList<T> pList, IEnumerable<T> pToAdd)
|
|
{
|
|
foreach (var iItem in pToAdd)
|
|
{
|
|
pList.AddIfNotIn(iItem);
|
|
}
|
|
}
|
|
|
|
public static void AddRange<T>(this IList<T> pList, IEnumerable<T> pToAdd)
|
|
{
|
|
foreach (T iItem in pToAdd)
|
|
{
|
|
pList.Add(iItem);
|
|
}
|
|
}
|
|
|
|
public static bool Exists<T>(this IList<T> pList, Predicate<T> pMatch)
|
|
{
|
|
foreach (T iItem in pList)
|
|
{
|
|
if (pMatch(iItem))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static void MakeEqualTo<T>(this IList<T> plist, IList<T> otherList)
|
|
{
|
|
var toRemove = new List<T>();
|
|
|
|
foreach (T t in plist)
|
|
{
|
|
if (!otherList.Contains(t))
|
|
{
|
|
toRemove.Add(t);
|
|
}
|
|
}
|
|
|
|
plist.RemoveRange(toRemove);
|
|
|
|
foreach (T t in otherList)
|
|
{
|
|
if (!plist.Contains(t))
|
|
{
|
|
plist.Insert(otherList.IndexOf(t), t);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void RemoveRange<T>(this IList<T> pList, IEnumerable<T> pToRemove)
|
|
{
|
|
foreach (T iItem in pToRemove.ToList())
|
|
{
|
|
pList.Remove(iItem);
|
|
}
|
|
}
|
|
|
|
public static void RemoveRange<T>(this IList<T> pList, Predicate<T> pMatch)
|
|
{
|
|
List<T> lToDelete = pList.Where(t => pMatch(t)).ToList();
|
|
pList.RemoveRange(lToDelete);
|
|
}
|
|
|
|
public static List<T> GetElementsInCommon<T>(List<List<T>> listCollection)
|
|
{
|
|
var result = new List<T>();
|
|
var allElements = new List<T>();
|
|
|
|
listCollection.DoForEach(list =>
|
|
{
|
|
allElements.AddRangeIfElementsNotIn(list);
|
|
});
|
|
|
|
allElements.DoForEach(element =>
|
|
{
|
|
if(listCollection.TrueForAll(list => list.Contains(element)))
|
|
{
|
|
result.AddIfNotIn(element);
|
|
}
|
|
});
|
|
|
|
return result;
|
|
}
|
|
|
|
public static List<T> Clone<T>(this IList<T> list)
|
|
{
|
|
return list?.ToList() ?? new List<T>();
|
|
}
|
|
|
|
public static void AddRangeIfElementsNotIn<T>(this IList<T> list, IList<T> toAdd)
|
|
{
|
|
foreach(var item in toAdd)
|
|
{
|
|
list.AddIfNotIn(item);
|
|
}
|
|
}
|
|
|
|
public static void AddRange<T>(this IList<T> list, IList<T> toAdd)
|
|
{
|
|
foreach(T item in toAdd)
|
|
{
|
|
list.Add(item);
|
|
}
|
|
}
|
|
|
|
public static bool AreListEqual<T>(this IList<T> list1, IList<T> list2)
|
|
{
|
|
if(list2 is null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return list1.Count == list2.Count && list1.All(list2.Contains) && list2.All(list1.Contains);
|
|
}
|
|
|
|
public static void AddIfNotNull<T>(this IList<T> list, T item)
|
|
{
|
|
if(item != null)
|
|
{
|
|
list.Add(item);
|
|
}
|
|
}
|
|
|
|
public static void AddIfNotNullAndNotIn<T>(this IList<T> list, T item)
|
|
{
|
|
if(item != null)
|
|
{
|
|
list.AddIfNotIn(item);
|
|
}
|
|
}
|
|
|
|
public static void AddRangeIfNotNull<T>(this IList<T> list, IList<T> items)
|
|
{
|
|
if(items == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
foreach(var item in items)
|
|
{
|
|
list.AddIfNotNull(item);
|
|
}
|
|
}
|
|
}
|
|
} |