title: InvalidOperationException in where clause when attempting to create projection with Compile/Invoke
body: <p>I've got a model and I want to build a simplified query model on top of it using projections.
  Here we have 2 related 'derived' classes.</p>
  <p>note - in the 2nd class ES_PROGRAMGUIDEDAYSCHEDULE, there are 2 implementations of of the projection, the uncommented explicit one works, the commented one out fails.</p>
  <pre class="lang-cs prettyprint-override"><code>class ESP_CHANNEL
  {
      public string? name { get; set; }
  
      public static Expression&lt;Func&lt;Channel, ESP_CHANNEL&gt;&gt; Projection
      {
          get
          {
              return x =&gt; new ESP_CHANNEL
              {
                  name = x.LotIdLeafoftreepartNavigation.Leaf
              };
          }
      }
  }
  
  class ES_PROGRAMGUIDEDAYSCHEDULE
  {
      public ESP_CHANNEL? ds_channel { get; set; }
  
      public DateTime? ds_date { get; set; }
  
      public static IQueryable&lt;ES_PROGRAMGUIDEDAYSCHEDULE&gt; Query(ModelContext context)
      {
          return context.Pgprogramguidedayschedules.Select(ES_PROGRAMGUIDEDAYSCHEDULE.Projection);
      }
  
      private static Expression&lt;Func&lt;Pgprogramguidedayschedule, ES_PROGRAMGUIDEDAYSCHEDULE&gt;&gt; Projection
      {
          get
          {
              return x =&gt; new ES_PROGRAMGUIDEDAYSCHEDULE
              {
                  ds_date = x.PgdsIdDayscheduleNavigation.DsTxdate,
                  // If I explicitly instantiate an ESP_CHANNEL it works
                  ds_channel = new ESP_CHANNEL
                  {
                      name = x.PgdsIdDayscheduleNavigation.DsIdScheduleNavigation.SchIdChannelNavigation.LotIdLeafoftreepartNavigation.Leaf
                  }
                  // note THIS seemingly equivalent implementation via compile/invoke fails
                  //ds_channel = ESP_CHANNEL.Projection.Compile().Invoke(x.PgdsIdDayscheduleNavigation.DsIdScheduleNavigation.SchIdChannelNavigation)
              };
          }
      }
  }
  </code></pre>
  <p>If I then run this query like this.</p>
  <pre class="lang-cs prettyprint-override"><code>var x =
      (from schedule in ES_PROGRAMGUIDEDAYSCHEDULE.Query(ds)
          where schedule.ds_channel.name == channel
          &amp;&amp; schedule.ds_date == dt
          select schedule).Take(1);
  
  var ys = x.ToArray();
  </code></pre>
  <p>the explicit implementation in ES_PROGRAMGUIDEDAYSCHEDULE.Projection, works</p>
  <pre class="lang-cs prettyprint-override"><code>ds_channel = new ESP_CHANNEL
  {
      name = x.PgdsIdDayscheduleNavigation.DsIdScheduleNavigation.SchIdChannelNavigation.LotIdLeafoftreepartNavigation.Leaf
  }
  </code></pre>
  <p>whilst the seemingly equivalent implemention, where the instantiation of the ESP_CHANNEL is held in a the projection expression fails</p>
  <pre class="lang-cs prettyprint-override"><code>ds_channel = ESP_CHANNEL.Projection.Compile().Invoke(x.PgdsIdDayscheduleNavigation.DsIdScheduleNavigation.SchIdChannelNavigation)
  </code></pre>
  <p>fails with</p>
  <pre><code>An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.EntityFrameworkCore.dll
  The LINQ expression 'DbSet&lt;Pgprogramguidedayschedule&gt;()
      .LeftJoin(
          inner: DbSet&lt;Wondayschedule&gt;(), 
          outerKeySelector: p =&gt; EF.Property&lt;decimal?&gt;(p, &quot;PgdsIdDayschedule&quot;), 
          innerKeySelector: w =&gt; EF.Property&lt;decimal?&gt;(w, &quot;Oid&quot;), 
          resultSelector: (o, i) =&gt; new TransparentIdentifier&lt;Pgprogramguidedayschedule, Wondayschedule&gt;(
              Outer = o, 
              Inner = i
          ))
      .LeftJoin(
          inner: DbSet&lt;Psischedule&gt;(), 
          outerKeySelector: p =&gt; EF.Property&lt;decimal?&gt;(p.Inner, &quot;DsIdSchedule&quot;), 
          innerKeySelector: p0 =&gt; EF.Property&lt;decimal?&gt;(p0, &quot;Oid&quot;), 
          resultSelector: (o, i) =&gt; new TransparentIdentifier&lt;TransparentIdentifier&lt;Pgprogramguidedayschedule, Wondayschedule&gt;, Psischedule&gt;(
              Outer = o, 
              Inner = i
          ))
      .LeftJoin(
          inner: DbSet&lt;Channel&gt;(), 
          outerKeySelector: p =&gt; EF.Property&lt;decimal?&gt;(p.Inner, &quot;SchIdChannel&quot;), 
          innerKeySelector: c =&gt; EF.Property&lt;decimal?&gt;(c, &quot;LotIdLeafoftreepart&quot;), 
          resultSelector: (o, i) =&gt; new TransparentIdentifier&lt;TransparentIdentifier&lt;TransparentIdentifier&lt;Pgprogramguidedayschedule, Wondayschedule&gt;, Psischedule&gt;, Channel&gt;(
              Outer = o, 
              Inner = i
          ))
      .Where(p =&gt; __Compile_0.Invoke(p.Inner).name == __channel_1 &amp;&amp; p.Outer.Outer.Inner.DsTxdate == __dt_2)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. 
  </code></pre>
  <p>any ideas?</p>
  <p>(the problem appears to be that its the where clause that fails to interpret the invoke correctly).</p>
  <h2>EDIT 1</h2>
  <p>note his query DOES execute correctly.</p>
  <pre class="lang-cs prettyprint-override"><code>var x =
      (from schedule in ES_PROGRAMGUIDEDAYSCHEDULE.Query(ds)
          //where schedule.ds_channel.name == channel
          //&amp;&amp; schedule.ds_date == dt
          select schedule).Take(1);
  </code></pre>
  <p>so it appears that it is the where clause navigating via ds_channel that is causing the issue.</p>