title: How to add properties to anonymous object dynamically?
body: <blockquote>
  <p><strong>Possible Duplicate:</strong><br />
  <a href="https://stackoverflow.com/questions/7437043/is-it-possible-to-declare-an-anonymous-type-in-c-with-a-variable-dynamic-set-of">Is it possible to declare an anonymous type in C# with a variable/dynamic set of fields?</a></p>
  </blockquote>
  
  <p>I've built a query builder that constructs a query by examining some GET values that the user has set in my ASP.NET MVC3 application. This query builder can add sorting, limiting and filtering (the WHERE clause) and then returns the constructed query.</p>
  <p>This query then goes to my Dapper repository that tries to execute the query with:</p>
  <pre><code>using (IDbConnection connection = new MySqlConnection(ConnectionStringFactory.GetConnectionString(Database.Push)))
  {
      connection.Open();
  
      return connection.Query&lt;Notification&gt;(query.QueryString,
                                            new 
                                            {
                                                someAnonymousStuffHere = SomeValue
                                            });
  }
  </code></pre>
  <p>However, the parameters for the query (it's a prepared statement) need to be put in an anonymous type. The problem is: I don't know how to construct this anonymous type dynamically. What I do now is pushing all the parameters into a Dictionary&lt;string, string&gt; in the query builder, where the key is the name of the property and the value is, well, the value.</p>
  <p>I can't just define an anonymous type in my query builder because the builder consists of several steps, of which some aren't executed. So sometimes we have 5 parameters, sometimes we have 3 parameters, etc.</p>
  <p>I hope I explained my problem clear enough.</p>