Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1073507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:00:03+00:00 2026-05-16T21:00:03+00:00

There are lots of Linq algorithms that only need to do one pass through

  • 0

There are lots of Linq algorithms that only need to do one pass through the input e.g. Select.

Yet all the Linq extension methods sit on IEnumerable rather than IEnumerator

    var e = new[] { 1, 2, 3, 4, 5 }.GetEnumerator(); 
    e.Select(x => x * x); // Doesn't work 

This means you can’t use Linq in any situation where you are reading from an “already opened” stream.

This scenario is happening a lot for a project I am currently working on – I want to return an IEnumerator whose IDispose method will close the stream, and have all the downstream Linq code operate on this.

In short, I have an “already opened” stream of results which I can convert into an appropriately disposable IEnumerator – but unfortunately all of the downstream code requires an IEnumerable rather than an IEnumerator, even though it’s only going to do one “pass”.

i.e. I’m wanting to “implement” this return type on a variety of different sources (CSV files, IDataReaders, etc.):

class TabularStream 
{ 
    Column[] Columns; 
    IEnumerator<object[]> RowStream; 
}

In order to get the “Columns” I have to have already opened the CSV file, initiated the SQL query, or whatever. I can then return an “IEnumerator” whose Dispose method closes the resource – but all of the Linq operations require an IEnumerable.

The best workaround I know of is to implement an IEnumerable whose GetEnumerator() method returns the one-and-only IEnumerator and throws an error if something tries to do a GetEnumerator() call twice.

Does this all sound OK or is there a much better way for me to implement “TabularStream” in a way that’s easy to use from Linq?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:00:04+00:00Added an answer on May 16, 2026 at 9:00 pm

    Using IEnumerator<T> directly is rarely a good idea, in my view.

    For one thing, it encodes the fact that it’s destructive – whereas LINQ queries can usually be run multiple times. They’re meant to be side-effect-free, whereas the act of iterating over an IEnumerator<T> is naturally side-effecting.

    It also makes it virtually impossible to perform some of the optimizations in LINQ to Objects, such as using the Count property if you’re actually asking an ICollection<T> for its count.

    As for your workaround: yes, a OneShotEnumerable would be a reasonable approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are lots of questions on how to improve communication between teams. One way
There are lots of IDEs for PHP development, but I'm curious about all the
I have heard that there is lots of interesting and useful WSGI middleware around.
Just getting my head around Linq and having lots of fun! Can any one
There's lots that can be taught about transactions in databases (and I'm really talking
There are lots of examples how to install windows service in one line: ManagedInstallClass.InstallHelper(
There are lots of PHP articles about the subject so is this a PHP
There are lots of web application frameworks available these days, for pretty much every
There are lots of non-image-based CAPTCHA ideas floating around. But what about the old-fashioned
There are lots of tools for creating installers on Windows (InstallShield, InnoSetup, NSIS, just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.