title: Linq to Entity Case Type of
body: <p>Imagine we have an <em>Invoice Item</em> Entity with following properties:</p>
  
  <p><strong>InvoiceItem</strong></p>
  
  <ul>
  <li>Id</li>
  <li>Date</li>
  <li>Number</li>
  </ul>
  
  <p><em>Invoice Item</em> has two different child entity (TPT), as following:</p>
  
  <p><strong>WarehouseInvoiceItem</strong> : <strong>InvoiceItem</strong></p>
  
  <ul>
  <li>Quantity</li>
  <li>Duration</li>
  </ul>
  
  <p><strong>FreightInvoiceItem</strong> : <strong>InvoiceItem</strong></p>
  
  <ul>
  <li>Weight</li>
  </ul>
  
  <p>Now, I want to fill out a DTO with following structure:</p>
  
  <p><strong>InvoiceItemDto</strong></p>
  
  <ul>
  <li>Id</li>
  <li>Date</li>
  <li>Number</li>
  <li>?Quantity</li>
  <li>?Duration</li>
  <li>?Weight</li>
  </ul>
  
  <p>This is how I'm trying to do so:</p>
  
  <pre><code>db.InvoiceItem.Select(i=&gt;new InvoiceItemDto{
     Id = i.Id,
     Date = i.Date, ...
  });
  </code></pre>
  
  <p>I can not access to the special fields of drived entities (Duration of WarehouseInvoiceItem for instance) because I can not use <code>ofType</code> like this:</p>
  
  <pre><code>db.InvoiceItem.ofType&lt;WarehouseInvoiceItem&gt;().Select()
  </code></pre>
  
  <p>Instead, I wish I could select like this:</p>
  
  <pre><code>db.InvoiceItem.Select(i=&gt;new InvoiceItemDto{
     Id = i.Id,
     Date = i.Date,
     Duration = (i is WarehouseInvoiceItem)? (WarehouseInvoiceItem)i.Duration : null,
     Quantity = (i is WarehouseInvoiceItem)? (WarehouseInvoiceItem)i.Quantity : null,
     Weight = (i is FreightInvoiceItem)? (FreightInvoiceItem)i.Weight : null
  });
  </code></pre>
  
  <p>Any idea how can I manage a query for this reason? Thank you in advanced.</p>