title: Optimize LINQ query that runs fast in Sql server?
body: <p>I want to calculate the rows of a related table:</p>
  
  <pre><code>MainTable tbl = tblInfo(id);
  var count = tbl.Related_Huge_Table_Data.Count();
  </code></pre>
  
  <p>The problem is: this takes too long (about 20 seconds) to execute, although when I run this query in Sql Server it executes below one second. How can I optimize this query in linq? I also tried to use stored procedure but no luck.</p>
  
  <p>This is the <code>tblInfo</code> method:</p>
  
  <pre><code>public MainTable tblInfo(int id)
  {
      MyDataContext context = new MyDataContext();
      MainTable mt = (from c in context.MainTables
                      where c.Id == id
                      select c).SingleOrDefault();
      return mt;
  }
  </code></pre>
  
  <p>I used LinqToSql and classes was generated by LinqToSql.</p>