title: Convert KeyValuePair to anonymous type in a LINQ query
body: <p>I have an <code>IEnumerable&lt;KeyValuePair&lt;string,string&gt;&gt;</code>, from which I would like, ideally, an anonymous object which has the keys as property names and the values as property values.</p>
  
  <p>I've tried various selection expressions (none of which even compiled...) and an approach using <code>ExpandoObject</code> (see below), but without success. Is there a good way to do this? If possible, I'd like to avoid an extra explicit iteration over the collection (i.e. do it all with a LINQ statement of some sort).</p>
  
  <p>This is what I've tried so far. I hope it also clarifies what I'm trying to do:</p>
  
  <pre><code>var kvps = getProps(); // returns IEnumerable&lt;KeyValuePair&lt;string,string&gt;&gt;
  
  dynamic o = new ExpandoObject();
  foreach (var kvp in kvps)
  {
      o.Add(kvp);
  }
  </code></pre>
  
  <p>This is OK at compile time, but at runtime I get  a <a href="http://en.wikipedia.org/wiki/YSOD#Yellow" rel="noreferrer">YSOD</a> stating <em>'System.Dynamic.ExpandoObject' does not contain a definition for 'Add'</em> - I guess it works at compile time because <code>o</code> is dynamic, so the compiler can't know if a method <code>.Add()</code> has been added to it since it was instantiated. The odd thing is, that on the <a href="http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject.aspx" rel="noreferrer">MSDN documenation page for ExpandoObject</a> <code>.Add()</code> is listed as one of several "explicitly implemented interface methods".</p>
  
  <p>It is not necessary for me to get this into a dynamic object - I just need to get something that has property names and values according to the keys and values of the key-value pairs.</p>
  
  <p><strong>Update:</strong> Well, this is embarrassing. Turns out this was something of an XY-problem too.</p>
  
  <p>I'm trying to render this to JSON using the built-in features of ASP.NET MVC, by simply returning <code>Json(data)</code> in my controller. The answers all worked very well to do what I first asked, but when I pass this object as <code>data</code> I still don't get what I want:</p>
  
  <pre><code>// What I get:
  [
     { Key: 'firstkey', Value: 'FirstValue' }, 
     { Key: 'secondKey', Value: 'secondValue' } 
  ]
  
  // What I want:
  { firstKey: 'FirstValue', secondKey: 'secondValue' }
  </code></pre>
  
  <p>Apparently, an <code>ExpandoObject</code> with the relevant properties added didn't cut it - it was cast to a dictionary before rendering...</p>