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

  • SEARCH
  • Home
  • 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 6255763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:25:37+00:00 2026-05-24T14:25:37+00:00

I was reading a piece of code from the XStreamingReader library (which seems like

  • 0

I was reading a piece of code from the “XStreamingReader” library (which seems like a really cool solution for being able to execute LINQ queries over XML documents but without loading the actual document into the memory (like in an XDocument object)
and was wondering about the following:

public IEnumerable<XElement> Elements()
{
    using (var reader = readerFactory())
    {
        reader.MoveToContent();
        MoveToNextElement(reader);
        while (!reader.EOF)
        {
            yield return XElement.Load(reader.ReadSubtree());
            MoveToNextFollowing(reader);
        }
    }
}

public IEnumerable<XElement> Elements(XName name)
{
    return Elements().Where(x => x.Name == name);
}

Regarding the 2nd method Elements(XName) – The method first calls Elements(), and then use Where() to filter it’s results, but i’m kind of intrigued about the order of executions in here since Elements() contains a yield statement.
From what I understand:
– Executing Elements() returns an IEnumerable collection, this collection physically does not contain any items YET.
– Where() is executed on that collection, behind the scene there’s a loop which iterates through every item, new items are “Loaded” on the fly, since yield is being used.
– All items which matched the Where statement are returned as an IEnumerable collection, and are PHYSICALLY IN that collection.

First, am I correct with the above assumption?
Second, in case i’m right – what if I wanted to return a “yielded” collection rather than returning a collection which is filled up physically with all the filtered data?
I’m asking this because it loses the entire purpose of NOT reading an entire “matching” block into the memory, but iterating one matching element at a time…

  • 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-24T14:25:37+00:00Added an answer on May 24, 2026 at 2:25 pm

    I assume when you say that items are physically in a collection, you mean that there is a structure in memory that contains all the items right now. With Where(), that’s not the case, it uses yield too internally (or something that acts the same as yield).

    When you try to fetch the first item, Where() iterates the source collection, until it finds the first item that matches. So, the elements are streamed both in Elements() and in Elements(XName) and the whole collection is never in memory, only piece by piece.

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

Sidebar

Related Questions

Recently I was reading the following piece of code from oracle collection tutorial when
I have a piece of code which looks like this: Snippet A: class Creature
I have this piece of code for reading from an excel cell: public T
From the below piece of code, why I am getting Reading Socket for response
So, I have this piece of code, which will just read the message from
I was reading a book on templates and found the following piece of code:
I'm reading 16-bit integers from a piece of hardware over the serial port. Using
I have a piece of code that needs to be run from a restricted
I have a piece of code that looks like this: Dir.new(path).each do |entry| puts
I came across this piece of code where programmer determines uploaded file size like

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.