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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:23:49+00:00 2026-05-12T21:23:49+00:00

My application constructs a Linq To Sql query and then populates a model which

  • 0

My application constructs a Linq To Sql query and then populates a model which is then passed to the view. The view then accesses the IEnumerable object (from the Linq to Sql query) to display the user interface. The problem I am having is that each time the View accesses the IEnumerable object from the model, the linq to sql database query is made. I want the database to be queried once.

My controller looks like this:

    IQueryable<Item> items = (from item in db.Items
                             orderby item.Name ascending
                             select item);
    ItemModel m = new ItemModel(items);

    return View("Index", m);

Where ItemModel is defined like this:

public class ItemModel
{

    public ItemModel(IEnumerable<Item> items)
    {
        this.Items = items;
    }

    public IEnumerable<Item> Items { get; private set; }

}

My View then does something like this

Item Count: <%= Html.Encode(Model.Items.Count())%>
<% foreach (var item in Model.Items) { %>
    <% Html.RenderPartial("ItemControl", item); %>
<% } %>

Whenever Model.Items is accessed in the view, I am seeing a database query. In the example above I would see two database queries for the two Model.Items calls (in the actual view Model.Items is accessed more than twice so the performance hit is much worse).

How should I restructure my view or model or controller to make only a single query to the database?

  • 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-12T21:23:49+00:00Added an answer on May 12, 2026 at 9:23 pm

    Change:

    ItemModel m = new ItemModel(items);
    

    To:

    ItemModel m = new ItemModel(items.ToArray());
    

    The idea is to perform the query and store the results in an array so that accessing those results multiple times only incurs 1 DB hit.

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

Sidebar

Related Questions

My application constructs a Parent object in a static factory, along with it's predetermined
I would need some basic vector mathematics constructs in an application. Dot product, cross
Application frameworks such as DotNetNuke, Eclipse, Websphere and so forth are available today which
My application dynamically loads assemblies at runtime from specific subfolders. These assemblies are compiled
I have created some extra functionality on my Linq-to-SQL classes to make things easier
My question is what is best practice to optimize performance using LINQ for SQL
I'm checking out Linq for NHibernate 2.1 to use in an ASP.NET MVC application
I'm looking to add an advanced search capability to my ASP.NET/SQL Server 2005 application.
I am using LINQ to SQL for my website to access two databases. The
This is my webservice code which performs constructs xml file and stores to particular

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.