using System.Collections.Concurrent; namespace MES.Service.util; public static class LinqThreadSafeExtensions { // 线程安全版本(基于ConcurrentDictionary) public static Func DistinctByKey( Func keySelector) { var seen = new ConcurrentDictionary(); return item => seen.TryAdd(keySelector(item), true); } // 扩展方法版本(推荐) public static IEnumerable DistinctByConcurrent( this IEnumerable source, Func keySelector) { var seen = new ConcurrentDictionary(); return source.Where(item => seen.TryAdd(keySelector(item), true)); } }