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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:03:59+00:00 2026-05-12T16:03:59+00:00

I use a foreach in my View to loop over my strongly-typed Model and

  • 0

I use a foreach in my View to loop over my strongly-typed Model and display a table. With only 25 rows and 7 columns, it takes about 280 msec. This seems slow. Is there some performance trick I should know for using loops in Views?

EDIT: My Controller takes data from an Azure Table and feeds it to a View using a ViewModel pattern. I don’t know if this matters, but my Views are in VB and my Models are in C#. So, they’re in separate projects. I’ll come up with a slimmed down example to post, but I’m running out the door now and will have to get to this later this evening. I was hoping I’d catch the StackOverflow crowd before people went home for the weekend, so my original post was made quickly without example code.

EDIT: I confirmed with Fiddler that there’s no lazy loading happening. There’s no Fiddler activity during the rendering of the View.

EDIT: If I feed a View with data from an Azure table, it takes 280 msec. If I feed the same View with fake data that looks just like real data from an Azure table, it takes 60 msec. In either case, the Controller just populates a ViewModel object and passes it to the View. The same ViewModel class is used in both instances. I don’t get it.

EDIT: I think I figured it out. Perhaps this would have been obvious to everyone else if I had included code at first. Here’s my ViewModel:

public class EmployeeChildrenViewModel
{
    public Employee employee;
    public IEnumerable<Child> children;
}

If I pass the above ViewModel to my View, the foreach takes 280 msec. If I populate the above ViewModel with children.ToList() first, then the View only takes 60 msec. However, upon further investigation I see that the overall page load time is the same in either case. I guess it doesn’t matter if the IEnumerable is iterated over in my Controller or my View since the overall effect is the same. I’m still not sure what iterating over children does since I know for sure that it’s not hitting the database at that point, as confirmed with Fiddler.

  • 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-12T16:04:00+00:00Added an answer on May 12, 2026 at 4:04 pm

    Are you using Linq-to-SQL or some other ORM that by default loads lazily?

    I suggest logging all database calls (datacontext.Log = some class that inherits from TextWriter) and check if associations are being loaded lazily when you iterate over them in the View.

    EDIT: It appears the below information is not relevant to this question, but I’ll leave it here as it might be useful to someone:

    Gonna assume you’re using Linq-to-SQL here (I’ll edit this if this is not the case):

    When your model foo has an association Bar (so foo.Bar) and you don’t specify any LoadOptions in the datacontext, the Bar is loaded lazily (so: loaded when called, like probably happens in your View) which means the View essentially goes to the database. And this happens for every row.

    Do something like the following:

    DataLoadOptions options = new DataLoadOptions();
    options.LoadWith<Foo>(f => f.Bar);
    context.LoadOptions = options;
    

    Further, to prevent this, try wrapping your DataContext in a using statement:

    using(DataContext context = new DataContext())
    {
    }
    

    Your View will now generate an exception when some association is loaded lazily because the DataContext will be disposed by now and unavailable for database operations.

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

Sidebar

Related Questions

I use a foreach loop inside ASP.NET MVC View page. For each element of
In performance point of view what should you use Nested foreach's or lambda/linq queries?
I'd like to use a foreach loop to insert a _POST associative array into
I'm trying to use a foreach loop for an array of objects. Inside of
In PHP I can use a foreach loop such that I have access to
I'm trying to use the foreach loop in an Ant script but I get
I store serialized data in the registry. I want to use a foreach loop
I have a view called volunteer_edit. Within volunteer_edit, I use jQuery to loop through
Just some architectural question: I use ASP.net MVC and exclusively rely on Strongly-Typed Views
How would you use simplemodal within a foreach loop so that it does not

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.