Extensionklassen

This commit is contained in:
Lyndon
2019-09-03 20:12:13 +02:00
parent 305a26370f
commit 1ebce8d36c
2 changed files with 137 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
using System.Collections.Generic;
using System.Linq;
namespace ChatController.Extensions
{
public static class IDictionaryTExtensions
{
public static IDictionary<T, List<U>> AddOrUpdateValueInDictionary<T, U>(this IDictionary<T, List<U>> pDictionary, T pKey, List<U> pValue)
{
if (!pDictionary.ContainsKey(pKey))
{
pDictionary.Add(pKey, pValue);
}
else
{
pDictionary[pKey] = pDictionary[pKey].Union(pValue).ToList();
}
return pDictionary;
}
public static IDictionary<T, List<U>> AddOrUpdateValueInDictionary<T, U>(this IDictionary<T, List<U>> pDictionary, T pKey, U pValue)
{
if (!pDictionary.ContainsKey(pKey))
{
pDictionary.Add(pKey, new List<U> { pValue });
}
else
{
pDictionary[pKey].AddIfNotIn(pValue);
}
return pDictionary;
}
public static void AddAndIgnoreDuplicates<T, U>(this IDictionary<T, U> pDictionary, T pKey, U pValue)
{
if (pDictionary.ContainsKey(pKey))
{
return;
}
pDictionary.Add(pKey, pValue);
}
public static void AddAndIgnoreDuplicates<T, U>(this IDictionary<T, U> pDictionary, IEnumerable<KeyValuePair<T, U>> pDictionary2Add)
{
foreach (var kvp in pDictionary2Add.Where(kvp => !pDictionary.ContainsKey(kvp.Key)))
{
pDictionary.Add(kvp);
}
}
}
}

View File

@@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace ChatController.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);
}
}
}