title: Count number of times database is accessed on a page?
body: <p>For context, I am a fairly new to ASP.Net, C#, and LINQ, and have recently come to be in charge of a site that is heavily depended on data from a database. Also, not sure if it matters, but the main app code is on a server separate from the DB.</p>
  
  <p><strong>My Question:</strong> Is there a way to "count" the number of times the application accesses the database on a given page? Pretty much all data is being accessed through LINQ (which has been set up to work with our database in a separate solution). On most pages within the site, there are multiple loops based on (changing) data from the DB, with each loop executing multiple LINQ queries inside.</p>
  
  <p>Basic Example from my code:</p>
  
  <pre><code>foreach(Answer ans in targetUser.UserAnswers.Select(ua =&gt; ua.Answer)
  {
    AnswerName.innerText = ans.Name;
    AnswerDesc.innerHtml = ans.Description;
    //etc
  }
  //etc
  </code></pre>
  
  <p>Except in reality, there are many loops (including further nested loops), and so it is really difficult to estimate how many times the database is being hit with every page load.</p>
  
  <p>I guess a followup question would be, is the database accessed 'separately' every time I use LINQ? I would imagine that every time we do "ans.Name" (for example), the database would have to be accessed again. In this case, I suppose one solution would be manually incrementing a count variable everywhere in the code where LINQ is used.</p>
  
  <p>If my question is confusing, please let me know and I will revise it. Thanks in advance!</p>