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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:24:51+00:00 2026-06-13T23:24:51+00:00

Why does my model go out to the DB again for related properties which

  • 0

Why does my model go out to the DB again for related properties which have already been loaded in a separate LINQ join statement?

Background info:

I recently ran some Performance profiling on my ASP.NET MVC3 Linq application I got some puzzling results that I hope to resolve.

Here is a simplified version of db structure. Foreign key relationships are set up.

Table Client:

ID|Client Name|Vertical ID|
___________________________
1 |Client1    |2          |
2 |Client2    |5          |
3 |Client3    |           |

Table Vertical

ID|Vertical Name|
_________________
2 |Life         |
5 |Guilt        |

When I display the list of clients, I am displaying them the client name and the vertical. My Controller is simple:

public ActionResult Index()
    {
        ...
    var model = repository.GetAllClients();
        return View(model);
    }

Get all clients runs the join on the vertical table (as well as a few others).

 public IEnumerable<client> GetAllClients()
    {
        OperationsMetricsDataContext db = new OperationsMetricsDataContext();
        var clients = from c in db.clients
                      join v in db.verticals on c.vertical equals v into tmp1
                      from v in tmp1.DefaultIfEmpty()
                      join sm in db.cel_sm_staffs on c.sm_id equals sm.cel_sm_id into tmp2
                      from sm in tmp2.DefaultIfEmpty()
                      join cel in db.cel_sm_staffs on c.cel_id equals cel.cel_sm_id into tmp3
                      from cel in tmp3.DefaultIfEmpty()
                      select c;
        return clients;
    }

In my View, I then attempt to display the vertical name I have bound using this method in my partial client class

public string getVerticalName()
    {
        return this.vertical == null ? "N/A" : this.vertical.vertical_name;
    }

The problem:

@foreach (var item in Model) {
<tr>
    <td>
        @Html.Raw(item.client_name)
    </td>
    <td>
        @Html.Raw(item.getVerticalName())
    </td>

The problem that I uncovered is that even though I specified the join in the original model, (and I’ve been able to verify that the vertical property has a value loaded before the model is passed to the view), each time I call the getVerticalName method, Linq automatically goes out to the db again to load the vertical so that I can access the vertical name. This is pretty inefficient because I end up with a separate db call for each client just to get their vertical information.

What am I missing here? Shouldn’t LINQ know that the property is already bound and not go out to the db to retrieve it or is there something I need to specify to tell LINQ that my information already exists in the model?

  • 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-13T23:24:52+00:00Added an answer on June 13, 2026 at 11:24 pm

    Not sure how it behaves in MVC, but perhaps your problem is lazy loading the related entity.
    You can force Linq-2-sql to eagerly load using LoadOptions.

    Try

    OperationsMetricsDataContext db = new OperationsMetricsDataContext();
    
    DataLoadOptions dlo = new DataLoadOptions();
    dlo.LoadWith<Client>(c => c.Vertical);
    db.LoadOptions = dlo;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to fix this error: {exception:Persistent class \Class org.myapp.model.Account does not seem
Does jquery/javascript have any event model that you can attach to your objects? Basically
Again pulling my hair out due to some Flex/AS3 weirdness. The following code does
I figured out that CUDA does not work in 64bit mode on my mac
How does one implement a multithreaded single process model in linux fedora under c
Does anyone know why I am getting this error: The model item passed into
Does anyone know what the esiest way to update the entity model after adding/deleting
How does Django decide what in the model will be assigned an _id, when
Simple question: does the Java memory/synchronization model guarantee atomic pointer writes? That is, if
When does the expression passed to DisplayFor and EditorFor need to use the model

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.