title: Using AutoMapper QueryableExtensions with Entity Framework in ASP.NET MVC causes an "Operation could destabilize the runtime" exception
body: <p>Consider the following code:</p>
  
  <pre><code>protected override IEnumerable&lt;IListModel&gt; GetListInternal(
      IQueryModel2&lt;Contact&gt; queryModel) {
      /// Causes exception
      return this.Query(queryModel).Project().To&lt;ContactListModel&gt;().AsEnumerable();
  
      /// Does not cause exception
      return Mapper.Map&lt;IQueryable&lt;Contact&gt;, IEnumerable&lt;ContactListModel&gt;&gt;(this.Query(queryModel)).ToList();
  }
  </code></pre>
  
  <p>When the first line returns I get the exception: <em>Operation could destabilize the runtime</em>. When the second line returns it works just fine. From what I can understand, the second return actually returns a concrete <code>IEnumerable&lt;ContactListModel&gt;</code>, but the first return does not. It doesn't matter if I append <code>.AsEnumerable()</code>, <code>ToArray()</code>, or <code>.ToList()</code> to the first return, I just keep getting the exception. Thus there has to be something about what <code>Project()</code> is returning. Whatever it returns may be an <code>IEnumerable&lt;ContactListModel&gt;</code> thus passing the method's return requirement, but it's concrete object is causing the exception when it's further processed down the pipe.</p>
  
  <p>Specifically, the collection that this method returns is passed onto a <code>ListWrapper&lt;TEntity&gt;</code> class that does the final processing. The exception is always thrown when I call <code>.Count()</code> on the collection.</p>
  
  <p>I'm desperatelly seeking guidance on resolving the issue with the projection because the second return is highly inefficient. From what MiniProfiler is telling the about the page this specific code is used on, I'm making 1204 SQL queries, with duplicates, and I think all of us can agree that that is a problem... Thanks in advance for any suggestions!</p>
  
  <p>Here's what <code>Query()</code> and <code>IQueryModel2&lt;TEntity&gt;</code> look like:</p>
  
  <pre><code>public IQueryable&lt;TEntity&gt; Query(
      IQueryModel2&lt;TEntity&gt; queryModel = null) {
      return this.QueryInternal(queryModel);
  }
  
  private IQueryable&lt;TEntity&gt; QueryInternal(
      IQueryModel2&lt;TEntity&gt; queryModel) {
      IQueryable&lt;TEntity&gt; entities = this.Context.Set&lt;TEntity&gt;();
  
      if (queryModel != null) {
          if (queryModel.Entities != null) {
              entities = queryModel.Entities.AsQueryable();
          }
  
          if (queryModel.Predicate != null) {
              entities = entities.AsExpandable().Where(queryModel.Predicate);
          }
      }
  
      return entities;
  }
  
  public interface IQueryModel2&lt;TEntity&gt; {
      IEnumerable&lt;TEntity&gt; Entities { get; set; }
  
      Expression&lt;Func&lt;TEntity, bool&gt;&gt; Predicate { get; set; }
  
      SearchPostModel Search { get; set; }
  }
  </code></pre>
  
  <p><strong>Update (1)</strong></p>
  
  <pre><code>public sealed class ContactListModel :
      ListModel {
      [Display(Order = 3)]
      public string Email { get; set; }
      [Display(Order = 1)]
      public string Name { get; set; }
      [Display(Order = 2)]
      public string Phone { get; set; }
      [Display(Order = 4)]
      public ContactType ContactType { get; set; }
  }
  
  internal sealed class ContactToContactListModel :
      Profile {
      protected override void Configure() {
          base.Configure();
  
          base.CreateMap&lt;Contact, ContactListModel&gt;()
              .ForMember(
                  d =&gt; d.Name,
                  o =&gt; o.MapFrom(
                      s =&gt; (s.FirstName + " " + s.LastName)))
              .IgnoreAllUnmapped();
      }
  }
  </code></pre>
  
  <p><strong>Update (2)</strong></p>
  
  <pre><code>public static class AutoMapperExtensions {
      public static IMappingExpression&lt;TSource, TDestination&gt; IgnoreAllUnmapped&lt;TSource, TDestination&gt;(
          this IMappingExpression&lt;TSource, TDestination&gt; expression) {
          if (expression == null) {
              return null;
          }
  
          return IgnoreAllUnmappedInternal&lt;TSource, TDestination&gt;(expression, typeof(TSource), typeof(TDestination));
      }
  
      private static IMappingExpression&lt;TSource, TDestination&gt; IgnoreAllUnmappedInternal&lt;TSource, TDestination&gt;(
          IMappingExpression&lt;TSource, TDestination&gt; expression,
          Type sourceType,
          Type destinationType) {
          IEnumerable&lt;string&gt; unmappedProperties = Mapper.GetAllTypeMaps().First(
              m =&gt;
                  m.SourceType.Equals(sourceType)
                  &amp;&amp; m.DestinationType.Equals(destinationType)).GetUnmappedPropertyNames();
  
          foreach (string unmappedProperty in unmappedProperties) {
              expression.ForMember(unmappedProperty, o =&gt; o.Ignore());
          }
  
          return expression;
      }
  }
  </code></pre>
  
  <p><strong>Update 3</strong></p>
  
  <p>So, I loaded the project's assemblies into LinqPad and redid the querying code in it. Thanks to the amazing <code>.Dump()</code> method from LinqPad I was able to observe what the objects being returned really were. So the first return returns a <code>DbQuery&lt;T&gt;</code> by default and the second returns an <code>IList&lt;T&gt;</code> by default. Ironically when I call the exact same sequence of code in LinqPad it self the exception is <strong>not</strong> thrown and I get the ListWrapper I was expecting. I'm really not sure why it doesn't work in ASP.NET MVC. Although I'm not even sure it's ASP.NET's fault because it's crashing in code that's a library project in a different assembly...</p>
  
  <p>I'm still at a loss on how to fix this exception since LinqPad doesn't exhibit the problem. Not sure if it matters, but I'm targeting .NET 4.5.2 in all of the projects in the solution, so I'm not sure if it's related to that?</p>