是否有IEnumerable实现只迭代它的源(例如LINQ)一次

zza*_*ndy 22 .net c# linq ienumerable

提供itemsq LINQ表达式的结果:

var items = from item in ItemsSource.RetrieveItems()
            where ...
Run Code Online (Sandbox Code Playgroud)

假设每个项目的生成需要一些不可忽略的时间.

有两种操作模式:

  1. 使用foreach将允许开始使用集合开头的项目,而不是最终可用的项目.但是,如果我们想稍后再次处理相同的集合,我们将不得不复制保存它:

    var storedItems = new List<Item>();
    foreach(var item in items){
        Process(item);
        storedItems .Add(item);
    }
    
    // Later
    foreach(var item in storedItems){
        ProcessMore(item);
    }
    
    Run Code Online (Sandbox Code Playgroud)

    因为如果我们刚刚制作的foreach(... in items)temsSource.RetrieveItems()会再次被召唤.

  2. 我们可以直接使用.ToList(),但这会迫使我们等待最后一项检索,然后才能开始处理第一项.

问题:是否有一个IEnumerable实现会像常规的LINQ查询结果一样首次迭代,但会在进程中实现,以便第二次foreach迭代存储的值?

Mar*_*age 11

一个有趣的挑战,所以我必须提供自己的解决方案.事实上,我的解决方案现在很有趣,版本3.版本2是我根据Servy的反馈进行的简化.然后我意识到我的解决方案有很大的缺点.如果缓存的可枚举的第一个枚举没有完成,则不会进行缓存.许多LINQ扩展喜欢First并且Take只会枚举足够的可枚举来完成工作,我不得不更新到版本3以使其与缓存一起工作.

问题是关于可枚举的后续枚举,它不涉及并发访问.不过我决定让我的解决方案线程安全.它增加了一些复杂性和一些开销,但应该允许在所有场景中使用该解决方案.

public static class EnumerableExtensions {

  public static IEnumerable<T> Cached<T>(this IEnumerable<T> source) {
    if (source == null)
      throw new ArgumentNullException("source");
    return new CachedEnumerable<T>(source);
  }

}

class CachedEnumerable<T> : IEnumerable<T> {

  readonly Object gate = new Object();

  readonly IEnumerable<T> source;

  readonly List<T> cache = new List<T>();

  IEnumerator<T> enumerator;

  bool isCacheComplete;

  public CachedEnumerable(IEnumerable<T> source) {
    this.source = source;
  }

  public IEnumerator<T> GetEnumerator() {
    lock (this.gate) {
      if (this.isCacheComplete)
        return this.cache.GetEnumerator();
      if (this.enumerator == null)
        this.enumerator = source.GetEnumerator();
    }
    return GetCacheBuildingEnumerator();
  }

  public IEnumerator<T> GetCacheBuildingEnumerator() {
    var index = 0;
    T item;
    while (TryGetItem(index, out item)) {
      yield return item;
      index += 1;
    }
  }

  bool TryGetItem(Int32 index, out T item) {
    lock (this.gate) {
      if (!IsItemInCache(index)) {
        // The iteration may have completed while waiting for the lock.
        if (this.isCacheComplete) {
          item = default(T);
          return false;
        }
        if (!this.enumerator.MoveNext()) {
          item = default(T);
          this.isCacheComplete = true;
          this.enumerator.Dispose();
          return false;
        }
        this.cache.Add(this.enumerator.Current);
      }
      item = this.cache[index];
      return true;
    }
  }

  bool IsItemInCache(Int32 index) {
    return index < this.cache.Count;
  }

  IEnumerator IEnumerable.GetEnumerator() {
    return GetEnumerator();
  }

}
Run Code Online (Sandbox Code Playgroud)

扩展名使用如下(sequenceIEnumerable<T>):

var cachedSequence = sequence.Cached();

// Pulling 2 items from the sequence.
foreach (var item in cachedSequence.Take(2))
  // ...

// Pulling 2 items from the cache and the rest from the source.
foreach (var item in cachedSequence)
  // ...

// Pulling all items from the cache.
foreach (var item in cachedSequence)
  // ...
Run Code Online (Sandbox Code Playgroud)

如果只枚举了枚举的部分枚举,则会有轻微的泄漏(例如cachedSequence.Take(2).ToList(),将使用的枚举器ToList将被处理,但基础源枚举器不会被丢弃.这是因为前两个项目被缓存且源枚举器保持活动应该请求后续项目.在这种情况下,源枚举器只在清除垃圾收集时才被清除(与可能的大缓存同时).

  • 放弃`IDisposable`对象是icky,虽然我想,因为没有人知道将来是否会有'GetEnumerator`的调用,可能没有什么好方法可以知道枚举器什么时候可以安全处置.太糟糕了,没有一次性可枚举的概念. (2认同)

gor*_*ric 8

查看Reactive Extentsions库 - 有一个MemoizeAll()扩展,它将在访问IEnumerable后缓存这些项目,并存储它们以供将来访问.

请参阅Bart De Smet的这篇博客文章,了解MemoizeAll其他Rx方法.

编辑:这实际上现在位于单独的Interactive Extensions包中 - 可从NuGetMicrosoft Download获得.