Count() 值中的 IEnumerable 批处理差异

IEnumerable batching discrepancy in Count() value

我正在尝试将 IEnumerable<T> 分成相等的子集并遇到以下解决方案:

  1. MoreLinq Nuget library 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);
    }
    }
    
  2. 另一种最佳解决方案可用在以下link(内存效率更高):

    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()时,也是错误的,因为我们仍然在处理第二种方法中的内存流并且没有分配内存,但仍然不正确的结果应该不是这样的,有什么意见/建议

第二个结果 return Count=10 的原因是因为它使用 while (enumerator.MoveNext()) 将产生 10 次并导致结果可枚举包含 10 个可枚举而不是 3.

参考问题中得分较高 的答案也为问题提供了合理的解决方案。