title: Entity Framework 4.1: Unable to cast from DbQuery to ObjectQuery
body: <p>I have the following code:</p>
  
  <pre><code>public void DeleteAccountsForMonth(int year, int month)
  {
      var result = from acm in this._database.AccountsOnMonth
                   where ((acm.Year == year) &amp;&amp; (acm.Month == month))
                   select acm.Id;
      var query = (ObjectQuery&lt;int&gt;)result;
  
      string sql = string.Format(
          "DELETE FROM [AccountsOnMonth] WHERE [AccountsOnMonth].[Id] IN ({0})",
          query.ToTraceString()
      );
  
      var parameters = new List&lt;System.Data.SqlClient.SqlParameter&gt;();
      foreach (ObjectParameter parameter in query.Parameters)
      {
          parameters.Add(new System.Data.SqlClient.SqlParameter {
              ParameterName = parameter.Name,
              Value = parameter.Value
          });
      }
  
      this._database.Database.ExecuteSqlCommand(sql, parameters.ToArray());
  }
  </code></pre>
  
  <p>Basically, what I'm trying to do is to delete a bulk of data from a context (get a query result, get SQL and execute it). But I'm having a problem when casting <code>result</code> to <code>ObjectQuery</code>. The exception that gives is</p>
  
  <blockquote>
    <p>Unable to cast object of type
    'System.Data.Entity.Infrastructure.DbQuery<code>1[System.Int32]' to type
    'System.Data.Objects.ObjectQuery</code>1[System.Int32]'.</p>
  </blockquote>
  
  <p>Can anybody give any hint to solve this? Thanks!</p>
  
  <p><strong>EDIT:</strong> Ladislav first solution helped me solve the problem, but it happenned a little problem with the SQL parameters of the generated SQL query, i.e. the SQL query generated by <code>query.ToString()</code> was this:</p>
  
  <pre><code>DELETE FROM [SncAccountOnMonths] WHERE [SncAccountOnMonths].[Id] IN (
      SELECT [Extent1].[Id] AS [Id]
      FROM [dbo].[SncAccountOnMonths] AS [Extent1]
      WHERE ([Extent1].[Year] = @p__linq__0) AND ([Extent1].[Month] = @p__linq__1))
  </code></pre>
  
  <p>The problem was that the variables <code>@p__linq__0</code> and <code>@p__linq__1</code> where not declared and so the query gave the error "Must declare the scalar variable @p_<em>linq</em>_0" (I sure it would give the same error for variable <code>@p__linq__1</code>). To "declare" them I need to pass them as arguments of the <code>ExecuteSqlCommand()</code>. And so, the final solution for the initial answer is the code below:</p>
  
  <pre><code>public void DeleteAccountsForMonth(int year, int month)
  {
      var result = (this._database.AccountsOnMonth
          .Where(acm =&gt; (acm.Year == year) &amp;&amp; (acm.Month == month)))
          .Select(acm =&gt; acm.Id);
      var query = (DbQuery&lt;int&gt;)result;
  
      string sql = string.Format(
          "DELETE FROM [AccountsOnMonth] WHERE [AccountsOnMonth].[Id] IN ({0})",
          query.ToString()
      );
  
      this._database.Database.ExecuteSqlCommand(sql,
          new SqlParameter("p__linq__0", year),
          new SqlParameter("p__linq__1", month)
      );
  }
  </code></pre>
  
  <p>By the way, I assume the variables generated always have the format <code>@p__linq__</code>, unless Microsoft's Entity Framework Team changes it in any future EF update...</p>