我正在尝试批量处理IEnumerable<T>
相等的子集并遇到以下解决方案:
MoreLinq Nuget 库 Batch,这里详细介绍了它的实现:
MoreLinq - Batch,在下面粘贴源代码:
public static IEnumerable<TResult> Batch<TSource, TResult>(this IEnumerable<TSource> source, int size, Func<IEnumerable<TSource>, TResult> resultSelector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (size <= 0) throw new ArgumentOutOfRangeException(nameof(size)); if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector)); return BatchImpl(source, size, resultSelector); } private static IEnumerable<TResult> BatchImpl<TSource, TResult> (this IEnumerable<TSource> source, int size,Func<IEnumerable<TSource>, TResult> resultSelector) { Debug.Assert(source != null); Debug.Assert(size > 0); Debug.Assert(resultSelector != null); TSource[] bucket = null; var count = 0; foreach (var item in source) { if (bucket == null) { bucket = new TSource[size]; } bucket[count++] = item; // The bucket is fully buffered before it's yielded if (count != size) { continue; } // Select is necessary so bucket contents are streamed too yield return resultSelector(bucket); bucket = null; count = 0; } // Return the last bucket with all remaining elements if (bucket != null && count > 0) { Array.Resize(ref bucket, count); yield return resultSelector(bucket); } }
以下链接提供了另一个最佳解决方案(内存效率更高):
IEnumerable Batching,在下面粘贴源代码:
public static class BatchLinq { public static IEnumerable<IEnumerable<T>> CustomBatch<T>(this IEnumerable<T> source, int size) { if (size <= 0) throw new ArgumentOutOfRangeException("size", "Must be greater than zero."); using (IEnumerator<T> enumerator = source.GetEnumerator()) while (enumerator.MoveNext()) yield return TakeIEnumerator(enumerator, size); } private static IEnumerable<T> TakeIEnumerator<T>(IEnumerator<T> source, int size) { int i = 0; do yield return source.Current; while (++i < size && source.MoveNext()); } }
这两种解决方案都提供了最终结果IEnumerable<IEnumerable<T>>
。
我在以下代码中发现了差异:
var result = Fetch IEnumerable<IEnumerable<T>>
从上面建议的任何一种方法
result.Count()
, 导致不同的结果,它对 MoreLinq Batch 是正确的,但对另一个不正确,即使结果是正确的并且两者都相同
考虑以下示例:
IEnumerable<int> arr = new int[10] {1,2,3,4,5,6,7,8,9,10};
For a Partition size 3
arr.Batch(3).Count(), will provide result 4 which is correct
arr.BatchLinq(3).Count(), will provide result 10 which is incorrect
即使提供的批处理结果是正确的,当我们这样做时ToList()
,由于我们仍在处理第二种方法中的内存流并且没有分配内存,但仍然不正确的结果不应出现错误,任何意见/建议