title: How to get real T-SQL generated by Entity Framework with ToTraceString() not working
body: <pre><code>    //get full list of active employees
  
      public static object EmployeeList()
      {
          string traceFile = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  
          var empList = (from emp in dbViews.EmployeeList
                         where emp.StatusID == 7
                         orderby emp.EmpNo
                         select new  
                         {
                             emp.id, 
                             emp.Name, 
                             emp.EmpNo,
                             emp.Telephone,
                             emp.EmployeeType,
                             emp.DepartmentName,
                             emp.Supervisor,
                             emp.ImmediateSupervisor,
                             emp.StatusID 
                         });
  
          File.AppendAllText(traceFile, ((ObjectQuery)empList).ToTraceString()); 
          return empList.ToList();
      }
  </code></pre>
  
  <p>When executing the above code, am getting the error below and am not sure why</p>
  
  <p>*</p>
  
  <blockquote>
    <p>Unable to cast object of type
    'System.Data.Entity.Infrastructure.DbQuery<code>1[&lt;&gt;f__AnonymousType0</code>9[System.Int32,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.Nullable`1[System.Int32]]]'
    to type 'System.Data.Objects.ObjectQuery'.</p>
  </blockquote>
  
  <p>*</p>
  
  <p>I just want to get the real T-SQL generated by the above Entity Framework code.
  The error is at line below </p>
  
  <pre><code>File.AppendAllText(traceFile, ((ObjectQuery)empList).ToTraceString());
  </code></pre>