title: Dynamic Selector Linq To Entities
body: <p>I have a dynamic selector expression that produces anonymous type. It's working fine in linq to objects, but in linq to entities, it throws:</p>
  
  <p><strong>Attempt 1</strong></p>
  
  <blockquote>
    <p>NotSupportedException</p>
    
    <p>Only parameterless constructors and initializers are supported in LINQ to Entities.</p>
  </blockquote>
  
  <pre><code>Expression&lt;Func&lt;User, T&gt;&gt; DynamicSelect&lt;T&gt;(T obj, ParameterExpression userParam)
  {
      var newExpression = Expression.New(
          typeof(T).GetConstructor(typeof(T).GenericTypeArguments),
          userParam,
          Expression.Constant("X"),
          Expression.Constant("Y")
      );
      return Expression.Lambda&lt;Func&lt;User, T&gt;&gt;(newExpression, userParam);
  }
  
  var userParam = Expression.Parameter(typeof(User), "u");
  var obj = new { User = new User(), Address = string.Empty, Fax = string.Empty };
  var arr = context.Set&lt;T&gt;()
      .Select(DynamicSelect(obj, userParam))
      .ToArray();
  </code></pre>
  
  <p><strong>Attempt 2</strong>, If I create a custom type, it's working, but I don't want to, because I want to reuse this helper method without creating additional custom type for each entity, I want to be able to pass the type based on consumer.</p>
  
  <pre><code>public class Container
  {
      public User User { get; set; }
      public string Address { get; set; }
      public string Fax { get; set; }
  }
  Expression&lt;Func&lt;User, T&gt;&gt; DynamicSelect&lt;T&gt;(T obj, ParameterExpression userParam)
  {
      var initExpression = Expression.MemberInit(
          Expression.New(typeof(T)),
          Expression.Bind(typeof(T).GetProperty("User"), userParam),
          Expression.Bind(typeof(T).GetProperty("Address"), Expression.Constant("X")),
          Expression.Bind(typeof(T).GetProperty("Fax"), Expression.Constant("Y"))
      );
      return Expression.Lambda&lt;Func&lt;User, T&gt;&gt;(initExpression, userParam);
  }
  
  var userParam = Expression.Parameter(typeof(User), "u");
  var arr = context.Set&lt;T&gt;()
      .Select(DynamicSelect&lt;Container&gt;(null, userParam))
      .ToArray();
  </code></pre>
  
  <p><strong>Attempt 3</strong>, I also tried using <code>Tuple&lt;User, string, string&gt;</code>, but it's not supported too.</p>
  
  <blockquote>
    <p>NotSupportedException</p>
    
    <p>LINQ to Entities does not recognize the method
    'System.Tuple`3[User,System.String,System.String]
    Create[User,String,String](User, System.String, System.String)'
    method, and this method cannot be translated into a store expression.</p>
  </blockquote>
  
  <pre><code>Expression&lt;Func&lt;User, T&gt;&gt; DynamicSelect&lt;T&gt;(T obj, ParameterExpression userParam)
  {
      var createExpression = Expression.Call(
          typeof(Tuple), 
          "Create", 
          typeof(T).GenericTypeArguments,
          userParam,
          Expression.Constant("X"), 
          Expression.Constant("Y"));
      return Expression.Lambda&lt;Func&lt;User, T&gt;&gt;(createExpression, userParam);
  }
  
  var userParam = Expression.Parameter(typeof(User), "u");
  var arr = context.Set&lt;User&gt;()
      .Select(DynamicSelect&lt;Tuple&lt;User, string, string&gt;&gt;(null, userParam))
      .ToArray();
  </code></pre>
  
  <p>Please help.</p>
  
  <p><strong>update</strong></p>
  
  <p>I was trying to reuse this helper method in any consumer (User, Customer, Associate, etc) without having specific implementation to each consumer.</p>
  
  <p>The class structure look like.</p>
  
  <pre><code>public class User
  {
      public int Id { get; set; }
      public string UserName { get; set; }
      public virtual ICollection&lt;Contact&gt; Contacts { get; set; }
  }
  public class Customer
  {
      public int Id { get; set; }
      public string CompanyName { get; set; }
      public virtual ICollection&lt;Contact&gt; Contacts { get; set; }
  }
  public class Contact
  {
      public int Id { get; set; }
      public string Type { get; set; }
      public string Content { get; set; }
  }
  public class UserDto
  {
      public int Id { get; set; }
      public string UserName { get; set; }
      public ContactDto Contact { get; set; }
  }
  public class CustomerDto
  {
      public int Id { get; set; }
      public string CompanyName { get; set; }
      public ContactDto Contact { get; set; }
  }
  public class ContactDto
  {
      public string Email { get; set; }
      public string Address { get; set; }
      public string Fax { get; set; }
      // other contact informations
  }
  </code></pre>
  
  <p>And I have many contacts that could be different for each consumer.</p>
  
  <pre><code>var users = context.Set&lt;User&gt;()
      .Select(x =&gt; new UserDto
      {
          Id = x.Id,
          UserName = x.UserName,
          Contact = new ContactDto
          {
              Email = x.Contacts.Where(c =&gt; c.Type == "Email").Select(c =&gt; c.Value).FirstOrDefault()
          }
      })
      .ToArray();
  
  var customers = context.Set&lt;Customer&gt;()
      .Select(x =&gt; new CustomerDto
      {
          Id = x.Id,
          CompanyName = x.CompanyName,
          Contact = new ContactDto
          {
              Address = x.Contacts.Where(c =&gt; c.Type == "Address").Select(c =&gt; c.Value).FirstOrDefault(),
              Fax = x.Contacts.Where(c =&gt; c.Type == "Fax").Select(c =&gt; c.Value).FirstOrDefault(),
          }
      })
      .ToArray();
  </code></pre>
  
  <p>And have refactored the <code>x.Contacts.Where(c =&gt; c.Type == "Address").Select(c =&gt; c.Value).FirstOrDefault()</code> into expression, but I can't use it directly inside the method like:</p>
  
  <pre><code>var users = context.Set&lt;User&gt;()
      .Select(x =&gt; new UserDto
      {
          Id = x.Id,
          UserName = x.UserName,
          Contact = new ContactDto
          {
              Email = GetContactExpression("Email").Compile()(x)
          }
      })
      .ToArray();
  </code></pre>
  
  <p>It will throw error because <code>Invoke</code> method is not supported in linq to expression, so that I need to refactored the whole <code>Select</code> expression, but I need to make it generic (<code>User</code> has <code>UserName</code>, but <code>Customer</code> has <code>CompanyName</code>, and any other information) and probably passing the contact type(s) too after this get solved. The expected result at the moment would be something lile:</p>
  
  <pre><code>var obj = new { User = new User(), Email = "" };
  var users = context.Set&lt;User&gt;()
      .Select(x =&gt; DynamicSelect(obj))
      .Select(x =&gt; new UserDto
      {
          Id = x.User.Id,
          UserName = x.User.UserName,
          Contact = new ContactDto
          {
              Email = x.Email
          }
      })
      .ToArray();
  </code></pre>