title: How to extract the Sql Command from a Compiled Linq Query
body: <p>In normal (not compiled) Linq to Sql queries you can extract the SQLCommand from the IQueryable via the following code:</p>
  
  <pre><code>SqlCommand cmd = (SqlCommand)table.Context.GetCommand(query);
  </code></pre>
  
  <p>Is it possible to do the same for a compiled query?</p>
  
  <p>The following code provides me with a delegate to a compiled query:</p>
  
  <pre><code>        private static readonly Func&lt;Data.DAL.Context, string, IQueryable&lt;Word&gt;&gt; Query_Get =
          CompiledQuery.Compile&lt;Data.DAL.Context, string, IQueryable&lt;Word&gt;&gt;(
              (context, name) =&gt;
                  from r in context.GetTable&lt;Word&gt;()
                  where r.Name == name
                  select r); 
  </code></pre>
  
  <p>When i use this to generate the IQueryable and attempt to extract the SqlCommand it doesn't seem to work. When debugging the code I can see that the SqlCommand returned has the 'very' useful CommandText of '<strong>SELECT NULL AS [EMPTY]</strong>'</p>
  
  <pre><code>        using (var db = new Data.DAL.Context())
          {
              IQueryable&lt;Word&gt; query = Query_Get(db, "word");
              SqlCommand cmd = (SqlCommand)db.GetCommand(query);
              Console.WriteLine(cmd != null ? cmd.CommandText : "Command Not Found");
          }
  </code></pre>
  
  <p>I can't find anything in google about this particular scenario, as no doubt it is not a common thing to attempt...</p>
  
  <p>So.... Any thoughts?</p>