Extensionklassen
This commit is contained in:
54
ChatController/Extensions/IDictionaryTExtensions.cs
Normal file
54
ChatController/Extensions/IDictionaryTExtensions.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
83
ChatController/Extensions/IListTExtensions.cs
Normal file
83
ChatController/Extensions/IListTExtensions.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user