using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Serein.Library.Utils { /// /// 对于 linq 的异步扩展方法 /// public static class LinqHelper { /// /// 根据条件筛选,只保留第一个满足条件的元素,其余的不包含。 /// public static IEnumerable DistinctByCondition( this IEnumerable source, Func predicate) { var seenKeys = new HashSet(); foreach (var item in source) { if (!predicate(item)) continue; /*var key = keySelector(item); if (seenKeys.Add(key)) // 如果是新键*/ yield return item; } } public static async Task> SelectAsync(this IEnumerable source, Func> method) { return await Task.WhenAll(source.Select(async s => await method(s))); } public static async Task> SelectAsync(this IEnumerable source, Func> method, int concurrency = int.MaxValue) { var semaphore = new SemaphoreSlim(concurrency); try { return await Task.WhenAll(source.Select(async s => { try { await semaphore.WaitAsync(); return await method(s); } finally { semaphore.Release(); } })); } finally { semaphore.Dispose(); } } } }