title: Querying data using Entity Framework from dynamically created table
body: <p><strong>TLDR; How do I read data from a table using Entity Framework, when the table name isn't known at compile-time?</strong></p>
  
  <p>There is an external system that processes a bulk of information, and then creates a new table for each batch run, and stores some data in that table. The column layout of these new tables are known before-hand, so I have generated an ADO.NET Entity Data Model <em>(edmx file)</em> from an existing database, where there is a table with the exact same column layout.</p>
  
  <p>The original table in that database is called <code>ResultTableTemplate</code>, so the entity class representing that table is also called <code>ResultTableTemplate</code>.</p>
  
  <p>I am trying to figure out how to use my ADO.NET Entity Data Model to read from those dynamically created tables, and getting back <code>IEnumerable&lt;ResultTableTemplate&gt;</code>. What I have done so far is this:</p>
  
  <pre><code>public IEnumerable&lt;ResultTableTemplate&gt; GetResultsFromTable(string tableName) {
      using (var context = new WorkdataEntities()) {
          var table = context.CreateQuery&lt;ResultTableTemplate&gt;("SELECT " +
              "ALL_THOSE_COLUMN_NAMES... " +
              "FROM " + tableName;
  
          var query = from item in table select item;
  
          return query.ToList();
      }
  }
  </code></pre>
  
  <p>When I run the query, I get a <code>System.Data.EntitySqlException</code> with the following message:</p>
  
  <blockquote>
    <p>'ResultTable419828' could not be resolved in the current scope or
    context. Make sure that all referenced variables are in scope, that
    required schemas are loaded, and that namespaces are referenced
    correctly. Near member access expression, line 1, column 225.</p>
  </blockquote>
  
  <p><code>ResultTable419828</code> is the value of <code>tableName</code></p>
  
  <p>I have tried <code>tableName + " AS ResultTableTemplate"</code> but it did not help.</p>
  
  <p>Is there a way forward for me, or will I have to do this without the help of Entity Framework?</p>
  
  <p><strong>EDIT:</strong> I realize now that the query text I am writing is not passed all the way down to the underlying SQL Server instance, but gets interpreted by Entity Framework which returns an <code>ObjectQuery&lt;ResultTableTemplate&gt;</code> instance, so it looks for <code>ResultTable419828</code> among the auto-generated <code>DbSet</code> instances of the Context.</p>
  
  <p>Still, is there a way for me do achieve what I need to do?</p>
  
  <p><strong>EDIT:</strong> Thanks <a href="https://stackoverflow.com/a/8897140/891667" title="Ladislav Mrnka">Ladislav Mrnka</a>. Now, I do this:</p>
  
  <pre><code>public IEnumerable&lt;ResultTableTemplate&gt; GetResultsFromTable(string tableName) {
      using (var context = new WorkdataEntities()) {
          var query = context.ExecuteStoreQuery&lt;ResultTableTemplate&gt;("SELECT " +
              "ALL_THOSE_COLUMN_NAMES... " +
              "FROM " + tableName;
  
          return query.ToList();
      }
  }
  </code></pre>