You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
publicstaticIEnumerable<TSource>DistinctBy<TSource,TKey>(thisIEnumerable<TSource>source,Func<TSource,TKey>keySelector){return source.DistinctBy(keySelector,null);}publicstaticIEnumerable<TSource>DistinctBy<TSource,TKey>(thisIEnumerable<TSource>source,Func<TSource,TKey>keySelector,IEqualityComparer<TKey>?comparer){if(source==null)thrownew ArgumentNullException(nameof(source));if(keySelector==null)thrownew ArgumentNullException(nameof(keySelector));return _();IEnumerable<TSource>_(){varknownKeys=newHashSet<TKey>(comparer);foreach(var element in source){if(knownKeys.Add(keySelector(element)))yieldreturnelement;}}}
使用了Count之后无法遍历集合,调试发现在Count内部调用了where的条件判断:
导致foreach遍历时迭代器再次判断where条件时HashSet内部已经有值
建议替换成System.Linq内的官方写法,实测没有问题
The text was updated successfully, but these errors were encountered: