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 7595029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:34:08+00:00 2026-05-30T21:34:08+00:00

I’m trying to understand what the affect of AsEnumerable() has over my data when

  • 0

I’m trying to understand what the affect of AsEnumerable() has over my data when iterating over it. I have a mock in-memory list. If I foreach over it with first calling ToList(), this forces evaluation and my printout looks like this (see code at the bottom of this post to explain output):

entering da
yield return
yield return
yield return
exiting da
doing something to aaron
doing something to jeremy
doing something to brendan

All makes sense. The ToList() forces the yields in the repository to execute first into a list, then we get our foreach iteration. All good so far.

When I do the same except use AsEnumerable(), based on what I’ve read regarding IQueryable (I understand this isn’t IQueryable), I would have thought this also forces evaluation, but it does not. It looks like this:

entering da
yield return
doing something to aaron
yield return
doing something to jeremy
yield return
doing something to brendan
exiting da

As it would if I never even called AsEnumerable(), so my question is:

  1. Why does AsEnumerable behave differently for an in memory collection vs linq to sql and its IQueryable return type?

  2. How would all this change when my repository is changed to using a SqlDataReader and doing a yield return inside of the reader (whilst calling Read() method). Would the rows coming from SqlServer that are buffered in the clients network buffer be fully evaluated before executing the foreach (normally a yield here will cause a “pause” in the repo while each row is processed by the foreach block. I know if I call ToList() first in this case, I can force evaluation of the SqlDataReader, so does AsEnumerable do the same here?

Note: I am not interested in whether putting yield into a SqlDataReader is a good idea, given it might hold the connection open, I’ve beaten this topic to death already 🙂

Here is my test code:

    public class TestClient
    {
        public void Execute()
        {
            var data = MockRepo.GetData();

            foreach (var p in data.AsEnumerable()) //or .ToList()
            {
                Console.WriteLine("doing something to {0}", p.Name);
            }

            Console.ReadKey();
        }
    }

    public class Person
    {
        public Person(string name)
        {
            Name = name;
        }

        public string Name { get; set; }
    }

    public class MockRepo
    {
        private static readonly List<Person> items = new List<Person>(3)
                        {
                            new Person("aaron"),
                            new Person("jeremy"), 
                            new Person("brendan")
                        };

        public static IEnumerable<Person> GetData()
        {
            Console.WriteLine("entering da");

            var enumerator = items.GetEnumerator();
            while (enumerator.MoveNext())
            {
                Console.WriteLine("yield return");
                yield return enumerator.Current;
            }

            Console.WriteLine("exiting da");
        }
    }
  • 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-30T21:34:10+00:00Added an answer on May 30, 2026 at 9:34 pm

    AsEnumerable does nothing except change the expression type to IEnumerable<T>. When it’s used in a query like this:

    var query = db.Customers
                  .Where(x => x.Foo)
                  .AsEnumerable()
                  .Where(x => x.Bar);
    

    … that just means you’ll use Queryable.Where for the first predicate (so that’s converted to SQL), and Enumerable.Where for the second predicate (so that’s executed in your .NET code).

    It doesn’t force evaluation. It doesn’t do anything. It doesn’t even check whether it’s called on null.

    See my Edulinq blog post on AsEnumerable for more information.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.