From fb2398a78544533897f773d637df792020830819 Mon Sep 17 00:00:00 2001 From: ElderJames Date: Wed, 20 Nov 2019 10:15:37 +0800 Subject: [PATCH] add extension methods for IEnumerable --- src/Shriek/CollectionExtensions.cs | 103 +++++++++++++++++++++++++++++ src/Shriek/ObjectExtensions.cs | 22 ------ 2 files changed, 103 insertions(+), 22 deletions(-) diff --git a/src/Shriek/CollectionExtensions.cs b/src/Shriek/CollectionExtensions.cs index 709e71e..b20c315 100644 --- a/src/Shriek/CollectionExtensions.cs +++ b/src/Shriek/CollectionExtensions.cs @@ -1,6 +1,8 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; namespace Shriek { @@ -62,6 +64,29 @@ public static void ForEach(this IEnumerable items, Action action) } } + public static async Task ForEachAsync(this IEnumerable items, Func func) + { + if (items == null) + { + return; + } + + for (int i = 0; i < items.Count(); i++) + { + await func(items.ElementAt(i)); + } + } + + public static ParallelLoopResult ForEachParallel(this IEnumerable items, Action action) + { + if (items == null) + { + throw new ArgumentNullException(nameof(items)); + } + + return Parallel.ForEach(items, action); + } + /// /// Checks whether or not collection is null or empty. Assumes colleciton can be safely enumerated multiple times. /// @@ -78,5 +103,83 @@ public static bool IsNullOrEmpty(this IEnumerable @this) return true; } + + /// + /// Check if an item is in a list. + /// + /// Item to check + /// List of items + /// Type of the items + public static bool IsIn(this T item, params T[] list) + { + return list.Contains(item); + } + + /// + /// Check if an item is in a list. + /// + /// Item to check + /// List of items + /// Type of the items + public static bool IsIn(this T item, IEnumerable list) + { + return list.Contains(item); + } + + public static IEnumerable Distinct(this IEnumerable source, Func comparer) + where T : class + { + return source.Distinct(new DynamicEqualityComparer(comparer)); + } + + public static IEnumerable OrderBy(this IEnumerable source, Func keySelector, Func comparer) + where T : class + where TKey : class + { + return source.OrderBy(keySelector, new DynamicComparer(comparer)); + } + + public static IEnumerable OrderByDescending(this IEnumerable source, Func keySelector, Func comparer) + where T : class + where TKey : class + { + return source.OrderByDescending(keySelector, new DynamicComparer(comparer)); + } + + private sealed class DynamicEqualityComparer : IEqualityComparer + where T : class + { + private readonly Func func; + + public DynamicEqualityComparer(Func func) + { + this.func = func; + } + + public bool Equals(T x, T y) + { + return this.func(x, y); + } + + public int GetHashCode(T obj) + { + return 0; + } + } + + private sealed class DynamicComparer : IComparer where T : class + { + private readonly Func func; + + public DynamicComparer(Func func) + { + this.func = func; + } + + public int Compare(T x, T y) + { + return this.func(x, y); + } + } } } \ No newline at end of file diff --git a/src/Shriek/ObjectExtensions.cs b/src/Shriek/ObjectExtensions.cs index ee1b72f..21741dc 100644 --- a/src/Shriek/ObjectExtensions.cs +++ b/src/Shriek/ObjectExtensions.cs @@ -170,27 +170,5 @@ public static T ToEnum(this string value, bool ignoreCase) return (T)Enum.Parse(typeof(T), value, ignoreCase); } - - /// - /// Check if an item is in a list. - /// - /// Item to check - /// List of items - /// Type of the items - public static bool IsIn(this T item, params T[] list) - { - return list.Contains(item); - } - - /// - /// Check if an item is in a list. - /// - /// Item to check - /// List of items - /// Type of the items - public static bool IsIn(this T item, IEnumerable list) - { - return list.Contains(item); - } } } \ No newline at end of file