title: How do I correctly use EF Core with AutoMapper ProjectTo and Unions?
body: <h2>My Setup</h2>
  
  <ul>
  <li>ASP.NET Core 2.0</li>
  <li>EntityFrameworkCore 2.0.1</li>
  <li>AutoMapper 6.2.2</li>
  </ul>
  
  <hr>
  
  <h2>Problem</h2>
  
  <p>I have a project with a DTO called <code>PersonDetail</code> and an Entity called <code>Person</code>. 
  When I call </p>
  
  <pre><code>db.People.Where(p =&gt; p.FirstName == "Joe").Union(db.People.Where(p =&gt; Age &gt; 30)).ProjectTo&lt;PersonDetail&gt;(mapperConfig).ToList(); 
  </code></pre>
  
  <p>I do not get the <code>PersonDetail</code> DTOs and Entity Framework (Core) throws an exception with the message:</p>
  
  <blockquote>
    <p>ArgumentException: The input sequence must have items of type 'Test.Module.Entities.Person', but it has items of type 'Test.Module.Dtos.PersonDetail'.</p>
  </blockquote>
  
  <hr>
  
  <h2>Example without the problem</h2>
  
  <p>When I run the code:</p>
  
  <pre><code> db.People.Where(p =&gt; p.FirstName == "Joe").Union(db.People.Where(p =&gt; Age &gt; 30)).ToList(); 
  </code></pre>
  
  <p>I get the <code>Person</code> entities with no exceptions.</p>
  
  <hr>
  
  <h2>The Execution Plans</h2>
  
  <p>Here is a working plan (with a union):</p>
  
  <blockquote>
    <p>{value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[Test.Module.Entities.Person]).Where(entity => ((entity != null) And ((63ed0ebd-2c02-4496-ac8d-b836cbf13259 == entity.CreatedBy) Or (393a6bb0-b437-4664-beb0-6800f509451b == entity.CreatedBy)))).Union(value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[Test.Module.Entities.Person]))}</p>
  </blockquote>
  
  <p>Now here is the same plan but with automapper projections too: </p>
  
  <blockquote>
    <p>{value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[Test.Module.Entities.Person]).Where(entity => ((entity != null) And ((63ed0ebd-2c02-4496-ac8d-b836cbf13259 == entity.CreatedBy) Or (393a6bb0-b437-4664-beb0-6800f509451b == entity.CreatedBy)))).Union(value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[Test.Module.Entities.Person])).Select(dto => new PersonDetail() {FirstName = dto.FirstName, LastName = dto.LastName, Deleted = dto.Deleted, Age = dto.Age, CreatedUtc = dto.CreatedUtc, CreatedBy = dto.CreatedBy, Id = dto.Id, RecordVersion = dto.RecordVersion, DisplayLabel = ((dto.FirstName + " ") + dto.LastName)})}</p>
  </blockquote>
  
  <hr>
  
  <h2>Note:</h2>
  
  <p>I'm only calling ToList to reduce this problem to it's smallest form. I understand that this doesn't seem like I need to use ProjectTo in this example. In my actual code, we are using OData and we need the final result to be a projected query with the DTOs as Queryable objects. I also understand that this Union is not really a good union example, bu again, just for simplification of the Union problem.</p>
  
  <p>Ia also opened issues on the respective GitHub projects:</p>
  
  <p><strong>EntityFrameworkCore</strong>: <a href="https://github.com/aspnet/EntityFrameworkCore/issues/11033" rel="noreferrer">https://github.com/aspnet/EntityFrameworkCore/issues/11033</a></p>
  
  <p><strong>AutoMapper</strong>: <a href="https://github.com/AutoMapper/AutoMapper/issues/2537" rel="noreferrer">https://github.com/AutoMapper/AutoMapper/issues/2537</a></p>