title: Exception on Inner LINQ query when calling ToList()
body: <p>Yesterday I was working on a code refactor and came across an exception that I really couldn't find much information on. Here is the situation.</p>
  
  <p>We have an a pair of EF entities that have a many to many relationship through a relation table. The objects in question look like this, leaving out the unnecessary bits. </p>
  
  <pre><code>public partial class MasterCode
  {
      public int MasterCodeId { get; set; }
      ...
  
      public virtual ICollection&lt;MasterCodeToSubCode&gt; MasterCodeToSubCodes { get; set; }
  }
  
  public partial class MasterCodeToSubCodes
  {
      public int MasterCodeToSubCodeId { get; set; }
      public int MasterCodeId { get; set; }
      public int SubCodeId { get; set; }
      ...
  }
  </code></pre>
  
  <p>Now, I attempted to run a LINQ query against these entities. We use a lot of LINQ projections into DTOs. The DTO and the query follow. masterCodeId is a parameter passed in.</p>
  
  <pre><code>public class MasterCodeDto
  {
      public int MasterCodeId { get; set; }
      ...
  
      public ICollection&lt;int&gt; SubCodeIds { get; set; }
  }
  
  (from m in MasterCodes
  where m.MasterCodeId == masterCodeId
  select new MasterCodeDto
  {
      ...
      SubCodeIds = (from s in m.MasterCodeToSubCodes
                    select s.SubCodeId).ToList(),
      ...
  }).SingleOrDefaultAsync();
  </code></pre>
  
  <p>The internal query throws the following exception</p>
  
  <pre><code>Expression of type 'System.Data.Entity.Infrastructure.ObjectReferenceEqualityComparer' cannot be used for constructor parameter of type 'System.Collections.Generic.IEqualityComparer`1[System.Int32]'
  </code></pre>
  
  <p>We have done inner queries like this before in other places in our code and not had any issues. The difference in this one is that we aren't new-ing up an object and projecting into it but rather returning a group of ints that we want to put in a list.</p>
  
  <p>I have found a workaround by changing the ICollection on MasterCodeDto to IEnumerable and dropping the ToList() but I was never able to find out why I couldn't just select the ids and return them as a list.</p>
  
  <p>Does anyone have any insight into this issue? Normally returning just an id field and calling ToList() works fine when it is not part of an inner query. Am I missing a restriction on inner queries that prevents an operation like this from happening? </p>
  
  <p>Thanks.</p>
  
  <p>Edit: To give an example of where this pattern is working I'll show you an example of a query that does work.</p>
  
  <pre><code> (from p in Persons
   where p.PersonId == personId
   select new PersonDto
   {
       ...
       ContactInformation = (from pc in p.PersonContacts
                             select new ContactInformationDto
                             {
                                 ContactInformationId = pc.PatientContactId,
                                 ...
                             }).ToList(),
       ...
    }).SingleOrDefaultAsync();
  </code></pre>
  
  <p>In this example, we are selecting into a new Dto rather than just selecting a single value. It works fine. The issues seems to stem from just selecting a single value.</p>
  
  <p>Edit 2: In another fun twist, if instead of selecting into a MasterCodeDto I select into an anonymous type the exception is also not thrown with ToList() in place.</p>