title: Conversion of Linq expressions from business to domain types?
body: <p>I am trying to figure out how to convert a Linq Expression between two types: One in my Business Repository and its related one in the Domain Repository.  Both types have the same data properties that will be used in the queries.  In my business layer, I have the following method that will perform a query based on a business entity:</p>
  
  <p>BUSINESS REPOSITORY: Domain entities converted to business entities</p>
  
  <pre><code>public IQueryable&lt;CustomerVendorProduct&gt; Query(Expression&lt;Func&lt;CustomerVendorProduct,bool&gt;&gt; filterInBusinessRepo)
  </code></pre>
  
  <p>So, a consumer could do something like the following:</p>
  
  <pre><code>var list = BusinessRepository.Query(c =&gt; c.CustomerId == 77);
  </code></pre>
  
  <p>They would get a list of CustomerVendorProduct business objects with CustomerId == 77.</p>
  
  <p>Within the Query() method of the Business Repository, I need to invoke the following method in the Domain Repository.  It will retrieve the raw, encrypted domain entities from the database.  Note that its query requires the entity type used in the Domain Repository.  It is related to our business entity, but simply is involved in CRUD operations.</p>
  
  <p>DOMAIN REPOSITORY: Raw, encrypted entities from the database</p>
  
  <pre><code>public IQueryable&lt;CustomerVendorProductEntity&gt; Query(Expression&lt;Func&lt;CustomerVendorProductEntity, bool&gt;&gt; filter)
  </code></pre>
  
  <p>So, the Query() method in the Business Repository would do something like the following:</p>
  
  <pre><code>var domainList = DomainRepository.Query(filterInBusinessRepo);
  </code></pre>
  
  <p>The result would be a list of CustomerVendorProductEntity objects.  The business repository then translates them into CustomerVendorBusiness entities and returns them to the caller.</p>
  
  <p>So, my question is how can I convert the Linq Expression from the Business Repository Entity Type: CustomerVendorProduct to the Domain Repository Entity Type: CustomerVendorProductEntity?  I think that I must create a conversion method... Something like the following... But, I am lost as to how to effect the conversion.  I would really appreciate your help:</p>
  
  <pre><code>    // Convert the Linq Expression from our Business Layer to our Domain Layer.
      private Expression&lt;Func&lt;CustomerVendorProductEntity, bool&gt;&gt; CVPToCVPE(Expression&lt;Func&lt;CustomerVendorProduct, bool&gt;&gt; filter)
      {
          var parms = System.Linq.Expressions.Expression.Parameter(typeof(CustomerVendorProductEntity), "destination");
  
          var result = System.Linq.Expressions.Expression.Lambda&lt;Func&lt;CustomerVendorProductEntity, bool&gt;&gt;(filter, parms);
  
          return result;
      } 
  </code></pre>
  
  <p>Thank you for your time and suggestions!
  Mike</p>
  
  <p>11/02/2012: Wow, no replies?  I would have thought that a lot of other developers have encountered this issue, especially within an MVC application...  Still hoping for help...</p>