Generate dynamic select lambda expressions <p>I am somewhat new to expression trees and I just don't quite understand some things.</p> <p>What I need to do is send in a list of values and select the columns for an entity from those values. So I would make a call something like this:</p> <pre><code>DATASTORE&lt;Contact&gt; dst = new DATASTORE&lt;Contact&gt;();//DATASTORE is implemented below. List&lt;string&gt; lColumns = new List&lt;string&gt;() { "ID", "NAME" };//List of columns dst.SelectColumns(lColumns);//Selection Command </code></pre> <p>I want that to be translated into code like this (<code>Contact</code> is an entity using the EF4):</p> <pre><code>Contact.Select(i =&gt; new Contact { ID = i.ID, NAME = i.NAME }); </code></pre> <p>So let's say I have the following code:</p> <pre><code>public Class&lt;t&gt; DATASTORE where t : EntityObject { public Expression&lt;Func&lt;t, t&gt;&gt; SelectColumns(List&lt;string&gt; columns) { ParameterExpression i = Expression.Parameter(typeof(t), "i"); List&lt;MemberBinding&gt; bindings = new List&lt;MemberBinding&gt;(); foreach (PropertyInfo propinfo in typeof(t).GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (columns.Contains(propinfo.Name)) { MemberBinding binding = Expression.Bind(propinfo, Expression.Property(i, propinfo.Name)); bindings.Add(binding); } } Expression expMemberInit = Expression.MemberInit(Expression.New(typeof(t)), bindings); return Expression.Lambda&lt;Func&lt;t, t&gt;&gt;(expMemberInit, i); } </code></pre> <p>When I ran the above code I got the following error: </p> <blockquote> <p><em>The entity or complex type 'Contact' cannot be constructed in a LINQ to Entities query.</em></p> </blockquote> <p>I looked at the body of the query and it emitted the following code:</p> <pre><code>{i =&gt; new Contact() {ID = i.ID, NAME = i.NAME}} </code></pre> <p>I am pretty sure that I should be able to construct the a new entity because I wrote this line explicitly as a test to see if this could be done:</p> <pre><code>.Select(i =&gt; new Contact{ ID = i.ID, NAME = i.NAME }) </code></pre> <p>This worked, but I need to construct the select dynamically.</p> <p>I tried decompiling a straight query(first time I have looked at the low level code) and I can't quite translate it. The high level code that I entered is:</p> <pre><code>Expression&lt;Func&lt;Contact, Contact&gt;&gt; expression = z =&gt; new Contact { ID = z.ID, NAME = z.NAME }; </code></pre> <p>Changing the framework used in the decompiler I get this code:</p> <pre><code>ParameterExpression expression2; Expression&lt;Func&lt;Contact, Contact&gt;&gt; expression = Expression.Lambda&lt;Func&lt;Contact, Contact&gt;&gt; (Expression.MemberInit(Expression.New((ConstructorInfo) methodof(Contact..ctor), new Expression[0]), new MemberBinding[] { Expression.Bind((MethodInfo) methodof(Contact.set_ID), Expression.Property(expression2 = Expression.Parameter(typeof(Contact), "z"), (MethodInfo) methodof(Contact.get_ID))), Expression.Bind((MethodInfo) methodof(Contact.set_NAME), Expression.Property(expression2, (MethodInfo) methodof(Contact.get_NAME))) }), new ParameterExpression[] { expression2 }); </code></pre> <p>I have looked several places to try and understand this but I haven't quite gotten it yet. Can anyone help?</p> <p>These are some places that I have looked:</p> <ul> <li><a href="http://blogs.msdn.com/b/meek/archive/2008/04/25/using-linq-expressions-to-generate-dynamic-methods.aspx" rel="nofollow">msdn blog</a> -- This is exactly what I want to do but my decopiled code soes not have Expression.Call.</li> <li><a href="http://msdn.microsoft.com/en-us/library/bb301790.aspx" rel="nofollow">msdn MemberInit</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/bb348662.aspx" rel="nofollow">msdn Expression Property</a></li> <li><a href="http://stackoverflow.com/questions/1531934/entity-framework-only-get-specific-columns">stackoverflow EF only get specific columns</a> -- This is close but this seems like it is doing the same thing as if i just use a select off of a query.</li> <li><a href="http://stackoverflow.com/questions/2719762/lambda-expression-to-be-used-in-select-query">stackoverflow lambda expressions to be used in select query</a> -- The answer here is exactly what I want to do, I just don't understand how to translate the decompiled code to C#.</li> </ul>