using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using BS.Shared.Core; namespace BS.Shared.Extensions { public static class IEnumerableTExtensions { public static bool ContainsSameItemsAs(this IEnumerable p1, IEnumerable pList) { if ((p1 == null || p1.Count() == 0) && (pList == null || pList.Count() == 0)) { return true; } if (p1 == null || pList == null || p1.Count() != pList.Count()) { return false; } foreach (T iT in p1) { bool lFound = false; foreach (T iT2 in pList) { if (iT.Equals(iT2)) { lFound = true; break; } } if (!lFound) { return false; } } return true; } public static IEnumerable DoForEach(this IEnumerable pList, Action pAction) { var list = pList as IList ?? pList.ToList(); foreach (var t in list) { pAction(t); } return list; } public static IEnumerable FlatOut(this IEnumerable source, Func> childListSelector) { var flattened = new List(); flattened.AddRange(source); while (flattened.Count > 0) { T current = flattened[0]; flattened.RemoveAt(0); IEnumerable childList = childListSelector(current); if (childList != null) { flattened.AddRange(childList); } yield return current; } } public static int IndexOf(this IEnumerable pList, T pItem) { int i = 0; foreach (T iT in pList) { if (iT.Equals(pItem)) { return i; } i++; } return -1; } public static ObservableCollection ToObservableCollection(this IEnumerable pList) { return new ObservableCollection(pList.ToList()); } public static ObservableSortCollection ToObservableSortCollection(this IEnumerable pList) { return new ObservableSortCollection(pList); } public static ObservableDictionary ToObservableDictionary(this Dictionary pList) { return new ObservableDictionary(pList); } public static ObservableSortCollection ToObservableSortCollection(this IEnumerable pList, Comparison pComparer) { return pComparer != null ? new ObservableSortCollection(pList, pComparer) : new ObservableSortCollection(pList); } public static string ToSeparatedString(this IEnumerable pList, string seperator) { var result = new StringBuilder(); var tmp = pList.ToArray(); for (var i = 0; i < tmp.Length; i++) { result.Append(tmp[i]); if (i < tmp.Length - 1) { result.Append(seperator); } } return result.ToString(); } public static HashSet ToHashSet(this IEnumerable pList) { return new HashSet(pList); } public static bool ContainsItems(this IEnumerable pList, IEnumerable pList2) { foreach (var item in pList2) { if (!pList.Contains(item)) { return false; } } return true; } } }