using System; using System.Collections; using System.Collections.Generic; using System.Linq; namespace Ips.Library.Basic { public static class EnumerableExtensions { public static bool IsNullOrEmpty(this IEnumerable @this) { return @this == null || !@this.Any(); } public static bool IsNotNullOrEmpty(this IEnumerable @this) { return @this != null && @this.Any(); } public static string JoinAsString(this IEnumerable source, string separator) { return source.Any() ? string.Join(separator, source) : ""; } public static string JoinAsString(this IEnumerable source, string separator) { return source.Any() ? string.Join(separator, source) : ""; } public static IEnumerable WhereIf(this IEnumerable source, bool condition, Func predicate) { return condition ? source.Where(predicate) : source; } public static IEnumerable WhereIf(this IEnumerable source, bool condition, Func predicate) { return condition ? source.Where(predicate) : source; } public static IEnumerable ForEach(this IEnumerable array, Action act) { foreach (var i in array) act(i); return array; } public static IEnumerable ForEach(this IEnumerable arr, Action act) { return arr.Cast().ForEach(act); } public static IEnumerable ForEach(this IEnumerable array, Func func) { var list = new List(); foreach (var i in array) { var obj = func(i); if (obj != null) list.Add(obj); } return list; } public static Dictionary> ToDictionary(this IEnumerable> groupings) { return groupings.ToDictionary(group => group.Key, group => group.ToList()); } } }