title: How to instantiate and initialize a dynamic object in expression tree?
body: <p>When working with <code>IQuerayble&lt;TItem&gt;</code> we can call <code>Select</code> like this:</p>
  
  <pre><code>query.Select( item =&gt; new { A=item.Prop1, B=item.Prop2});
  </code></pre>
  
  <p>And <code>Select</code> method expects <code>Expression&lt;Func&lt;TItem,TResult&gt;&gt;</code></p>
  
  <p>I need to use <code>ExpandoObject</code> instead of anonymous but statically typed class.<br/></p>
  
  <p>If it were possible it would look like:</p>
  
  <pre><code>query.Select( item =&gt; dynamic new ExpandoBoject { A=item.Prop1, B=item.Prop2});
  </code></pre>
  
  <p>So I want to construct expression tree <code>Expression&lt;Func&lt;TItem,ExpandoObject&gt;&gt;</code> where object's properties are initialized in the similar way as with anonymous type.<br/>
  Dynamic functionality is only needed for initialization so it's ok that Func returns <code>ExpandoObject</code> instead of <code>dynamic</code>.<br/></p>
  
  <p>I cannot find much documentation about <code>Expression.Dynamic</code> and corresponding binders I should use.</p>
  
  <hr>
  
  <p><strong>Update 1</strong></p>
  
  <p>Why do I need all this stuff?<br/>
  Because I want to <a href="https://stackoverflow.com/questions/18064064/how-to-retrieve-entity-keys-without-materializaing-entities">get primary keys</a>.<br/>
  I want to do it for any entity type.</p>
  
  <p>I know how to get the list of the properties composing PK, but now I need to make a tricky projection of the entity to <code>EntityKey</code>. Well, may be to same equvalent of this class.</p>
  
  <pre><code>var keys = context.Set&lt;TEntity&gt;().Where(Expression&lt;Func&lt;TEntity,bool&gt;).Select(Expression&lt;Func&lt;TEntity,EntityKey&gt;&gt;);
  </code></pre>
  
  <p>As I noted in the comments lambdas containing blocks cannot be converted to expression trees so i cannot simple create the dictionary and fill it. Now I'm playing with the expression tree semantically close to this code:</p>
  
  <pre><code>var dict = new Dictionary&lt;string,object&gt;();
  dict.Add("Prop1",value1);
  dict.Add("Prop2",value2);
  return dict
  </code></pre>
  
  <p>But I doubt EF can parse the expression containing blocks. Need to check.<br/>
  And I'm curious whether it will work with dynamic objects and Expression.MemberInit as it works with static objects.</p>
  
  <hr>
  
  <p><strong>Update 2</strong></p>
  
  <p>Entity Framework does not support dictionary initialization syntax.<br/>
  It throws <code>NotSupportedException</code> with the message: <em>Only list initializer items with a single element are supported in LINQ to Entities.</em></p>
  
  <hr>
  
  <p><strong>Update 3</strong></p>
  
  <p>EF does not support block expressions aswell.<br/>
  <code>NotSupportedException</code> with message: <em>Unknown LINQ expression of type 'Block'.</em></p>