title: How to create an anonymous object with property names determined dynamically?
body: <p>Given an array of values, I would like to create an anonymous object with properties based on these values.  The property names would be simply <code>"pN"</code> where <code>N</code> is the index of the value in the array.</p>
  
  <p>For example, given</p>
  
  <p><code>object[] values = { 123, "foo" };</code></p>
  
  <p>I would like to create the anonymous object</p>
  
  <p><code>new { p0 = 123, p1 = "foo" };</code></p>
  
  <p>The only way I can think of to do this would be to to use a <code>switch</code> or <code>if</code> chain up to a reasonable number of parameters to support, but I was wondering if there was a more elegant way to do this:</p>
  
  <pre><code>object[] parameterValues = new object[] { 123, "foo" };
  dynamic values = null;
  
  switch (parameterValues.Length)
  {
      case 1:
          values = new { p0 = parameterValues[0] };
          break;
      case 2:
          values = new { p0 = parameterValues[0], p1 = parameterValues[1] };      
          break;
      // etc. up to a reasonable # of parameters
  }
  </code></pre>
  
  <p><em>Background</em></p>
  
  <p>I have an existing set of methods that execute sql statements against a database.  The methods typically take a <code>string</code> for the sql statement and a <code>params object[]</code> for the parameters, if any.  The understanding is that if the query uses parameters, they will be named <code>@p0, @p1, @p2, etc.</code>.</p>
  
  <p>Example:</p>
  
  <pre><code>public int ExecuteNonQuery(string commandText, CommandType commandType, params object[] parameterValues) { .... }
  </code></pre>
  
  <p>which would be called like this:</p>
  
  <pre><code>db.ExecuteNonQuery("insert into MyTable(Col1, Col2) values (@p0, @p1)", CommandType.Text, 123, "foo");
  </code></pre>
  
  <p>Now I would like to use <a href="http://code.google.com/p/dapper-dot-net/">Dapper</a> within this class to wrap and expose Dapper's <code>Query&lt;T&gt;</code> method, and do so in a way that would be consistent with the existing methods, e.g. something like:</p>
  
  <pre><code>public IEnumerable&lt;T&gt; ExecuteQuery&lt;T&gt;(string commandText, CommandType commandType, params object[] parameterValues) { .... }
  </code></pre>
  
  <p>but Dapper's <code>Query&lt;T&gt;</code> method takes the parameter values in an anonymous object:</p>
  
  <pre><code>var dog = connection.Query&lt;Dog&gt;("select Age = @Age, Id = @Id", new { Age = (int?)null, Id = guid }); 
  </code></pre>
  
  <p>leading to my question about creating the anonymous object to pass parameters to Dapper.</p>
  
  <hr>
  
  <p>Adding code using the <code>DynamicParameter</code> class as requested by @Paolo Tedesco.</p>
  
  <pre><code>string sql = "select * from Account where Id = @p0 and username = @p1";
  dynamic values = new DynamicParameter(123, "test");
  var accounts = SqlMapper.Query&lt;Account&gt;(connection, sql, values);
  </code></pre>
  
  <p>throws an exception at line 581 of Dapper's SqlMapper.cs file:</p>
  
  <pre><code>using (var reader = cmd.ExecuteReader())
  </code></pre>
  
  <p>and the exception is a <code>SqlException</code>:</p>
  
  <blockquote>
    <p>Must declare the scalar variable "@p0".</p>
  </blockquote>
  
  <p>and checking the <code>cmd.Parameters</code> property show no parameters configured for the command.</p>