title: C# System.Linq.Dynamic: How to pass in the object [] params to SqlQuery
body: <p>Why does:</p>
  
  <pre><code>IEnumerable&lt;MyEntity&gt; MyFunction(string sql, object [] params)
  {
      // EG:
      // "SELECT * FROM MyTable WHERE MyField0 = @0 AND MyField1 = @1"
      // params = { 1, "UK" }
  
      return Context.Database.SqlQuery&lt;TEntity&gt;(sql, params);
  }
  </code></pre>
  
  <p>...give me the following error?:  </p>
  
  <pre><code>Must declare the scalar varliable "@0"
  </code></pre>
  
  <p>I know it would work if I did:</p>
  
  <pre><code>var query = Context.Database.SqlQuery&lt;TEntity&gt;(
     "SELECT * FROM MyTable WHERE MyField0 = @0 AND MyField1 = @1", 1, "UK"
  );
  </code></pre>
  
  <p>... but I want to abstract the code and call it as, for example:</p>
  
  <pre><code>var x = MyFunction(
         "SELECT * FROM MyTable WHERE MyField0 = @0 AND MyField1 = @1", 1, "UK"
      );
  </code></pre>
  
  <p>Ie, my problem is that I can't figure out how to pass in an array of params.</p>
  
  <h2>EDIT:</h2>
  
  <p>The accepted answer answers the question, but there is another problem in there:  you need to parametrize the array, ie, instead of just the values, pass them as an array of named SqlParameters.  See:</p>
  
  <p><a href="https://stackoverflow.com/questions/4873607/how-to-use-dbcontext-database-sqlquerytelementsql-params-with-stored-procedu">SqlQuery and parameters</a></p>