Using LINQ for an object that only has GetEnumerator()

Viewed 19769

Can you use LINQ in an object that exposes only Add(), Remove(), Count(), Item() and GetEnumerator() from System.Collections.IEnumerator?

4 Answers

Use a helper class:

namespace System.Linq {
  public sealed class EnumerableWrapper<T> : IEnumerable<T> {
    readonly IEnumerator<T> Enumerator;
    public EnumerableWrapper(IEnumerator<T> enumerator) => Enumerator = enumerator;
    public IEnumerator<T> GetEnumerator() => Enumerator;
    Collections.IEnumerator Collections.IEnumerable.GetEnumerator() => GetEnumerator();
  }
}

Then use Linq on a new instance of EnumerableWrapper

Related