title: Linq To Entities 5 re-use Expression<TEntity, TResult> in another entity
body: <p>I've been trying to find this, but I've not had luck.  Say I have a database with 2 tables, person and address.</p>
  
  <pre><code>table person
  id int
  name varchar(50)
  addressId int
  
  table address
  id int
  street varchar(50)
  country varchar(50)
  </code></pre>
  
  <p>In my data layer, I have a business object for Address, which is exposed to external callers.  I found an expression that I could use to centralize my creation code at the SQL level.  This way I don't have to write:</p>
  
  <pre><code>db.Address.Select( x =&gt; new Biz.Address{ street = x.street} ).ToList();  //and all the other properties etc
  </code></pre>
  
  <p>everywhere.  Instead I can now do: </p>
  
  <pre><code>db.Address.Select(AddressDto.ToDto).ToList();
  </code></pre>
  
  <p>Using this code:</p>
  
  <pre><code>internal static class AddressDto
  {
      internal static readonly Expression&lt;Func&lt;Address, Biz.Address&gt;&gt; ToDto =
          src =&gt; new Biz.Address
                     {
                         Id = src.id,
                         Street = src.street,
                         Country = src.country
                     };
  }
  </code></pre>
  
  <p>The problem is now that I am trying to do the same thing for the Person object, and I want to re-use this method to fill in the address.  However I can't seem to utilize an expression in it.</p>
  
  <pre><code>class Person
  {
      int Id;
      string Name;
      Address address;
  }
  
  internal static class PersonDto
  {
      internal static readonly Expression&lt;Func&lt;Person, Biz.Person&gt;&gt; ToDto =
          src =&gt; new Biz.Person
                     {
                         Id = src.id,
                         Name = src.name,
                         address =  src.Address  //How do i set this to an expression?
                     };
  }
  </code></pre>
  
  <p>The reason I ask for the expression, is because while it compiles fine if I use a normal method, it blows up at runtime, because it can't translate that to the object store.  However, if I do:</p>
  
  <pre><code>address = AddressDto.ToDto(src.Address)  
  </code></pre>
  
  <p>the compiler rejects that, as it wants a method, delegate, or event.  I'd love to find a way to do this.  the idea I'm trying to implement is to basically centralize the code that maps the Entity to the business object, so that my other code is kept clean and maintenance is easier when the schema changes.  If there is a different method signature I have to create and maintain, that'd be fine, as I'd place it in the same file and live with it.  I just can't seem to find the magic combination that'll make this work.</p>
  
  <p>Thanks!</p>
  
  <h2>Update from khan's suggestion.</h2>
  
  <p>That just moves this to a linq to objects area.  I am trying to do this in the SQL end because of how some queries are built up, we don't want to call .ToList till later.</p>
  
  <p>if I do:</p>
  
  <pre><code>internal static class PersonDto
  {
      internal static readonly Func&lt;Person, Biz.Person&gt; ToDto =
          src =&gt; new Biz.Person
                 {
                     Id = src.id,
                     Name = src.name,
                     address =  new Biz.Address { id = src.Address.Id }
                 };
       }
  </code></pre>
  
  <p>Then that works.  i just want to replace the new call.</p>