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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:07:37+00:00 2026-06-13T12:07:37+00:00

Given a very basic LINQ that is returned to a MVC view, at what

  • 0

Given a very basic LINQ that is returned to a MVC view, at what exact point does the deferred execution fire?

In the controller:

public ActionResult Index()
{
    var model = _fooService.GetAll();
    return View(model);
}

In the model:

@foreach (var item in Model) {    
    <tr>
        <td>@item.Bar</td>
   </tr>
}

The query is not executed when we call _fooService.GetAll() of course, but is deferred to some later point – but at which exact point is it executed?

  • The return View(model); statement in the controller (doesn’t look like it)?
  • The @foreach (var item in Model) line in the view?
  • The first time the @item.Bar line in the view is hit?
  • Something else that occurs in between return View(model); and the view being rendered?
  • 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-06-13T12:07:38+00:00Added an answer on June 13, 2026 at 12:07 pm

    MSDN documentation addresses this question under the deferred query execution section (emphasis mine).

    In a query that returns a sequence of values, the query variable
    itself never holds the query results and only stores the query
    commands. Execution of the query is deferred until the query variable
    is iterated over in a foreach or For Each loop
    …

    That narrows down the answer to options 2 and 3.

    foreach is just syntactic sugar, underneath the compiler re-writes that as a while loop. There’s a pretty thorough explanation of what happens here. Basically your loop will end up looking something like this

    {
      IEnumerator<?> e = ((IEnumerable<?>)Model).GetEnumerator();
      try
      { 
        int m;  // this is inside the loop in C# 5
        while(e.MoveNext())
        {
          m = (?)e.Current;
          // your code goes here
        }
      }
      finally
      { 
        if (e != null) ((IDisposable)e).Dispose();
      }
    }
    

    Enumerator is advanced before it reaches your code inside the loop, so slightly before you get to @item.Bar. That only leaves option 2, the @foreach (var item in Model) line (though technically that line doesn’t exist after the compiler is done with your code).

    I’m not sue if the query will execute on the call to GetEnumerator() or on the first call to e.MoveNext().


    As @pst points out in the comments, there are other ways to trigger execution of a query, such as by calling ToList, and it may not internally use a foreach loop. MSDN documentation sort of addresses this here:

    The IQueryable interface inherits the IEnumerable interface so that if
    it represents a query, the results of that query can be enumerated.
    Enumeration causes the expression tree associated with an IQueryable
    object to be executed.
    The definition of “executing an expression
    tree” is specific to a query provider. For example, it may involve
    translating the expression tree to an appropriate query language for
    the underlying data source. Queries that do not return enumerable
    results are executed when the Execute method is called.

    My understanding of that is an attempt to enumerate the expression will cause it to execute (be it through a foreach or some other way). How exactly that happens will depend on the implementation of the provider.

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

Sidebar

Related Questions

I'm learning clojure and have a very basic question: given that clojure has type
I'm trying to do some very basic time math - basically, given inputs of
I trying to do some very basic string processing in C (e.g. given a
Sorry for the very basic question, but this is actually a 2-part question: Given
I need some help with something that is probably very basic. I'm working on
I'm struggling with some very basic/conceptual problem with Moq and StructureMap. Given the following
I have this really basic code in a MVC controller action. It maps an
Given the following code snippet from Anthony Williams . A very basic tuple example
I am trying to create a very basic MVC framework to better understand the
A very new programmer to MVC, JSON & LINQ - I have created an

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.