title: Searching Against Projected EF Core Navigation Properties
body: <p>Using EF Core 3.1, I'm trying to perform a search against a nullable navigation property (a.k.a. left join) after it has been projected to a different type.</p>
  <p>For example:</p>
  <pre><code>var dtos = await query
      .Select(x =&gt; new RootEntityDto
      {
          // ...
  
          Nested = x.NestedId.HasValue
              ? new NestedEntityDto
              {
                  // ...
  
                  Description = x.Nested.Description
              }
              : null
      })
      .Where(x =&gt; x.Nested.Description.Contains(&quot;2&quot;))
      .ToArrayAsync();
  </code></pre>
  <p>The problem is, it gives me an error over the ternary that may conditionally result in null:</p>
  <pre><code>The LINQ expression 'DbSet&lt;RootEntity&gt;
      .LeftJoin(
          outer: DbSet&lt;NestedEntity&gt;, 
          inner: f =&gt; EF.Property&lt;Nullable&lt;int&gt;&gt;(f, &quot;NestedId&quot;), 
          outerKeySelector: o =&gt; EF.Property&lt;Nullable&lt;int&gt;&gt;(o, &quot;Id&quot;), 
          innerKeySelector: (o, i) =&gt; new TransparentIdentifier&lt;RootEntity, NestedEntity&gt;(
              Outer = o, 
              Inner = i
          ))
      .Where(f =&gt; EF.Property&lt;Nullable&lt;int&gt;&gt;(f.Inner, &quot;Id&quot;) != null ? new NestedEntityDto{ 
          Description = f.Inner.Description, 
      }
       : null.Description.Contains(&quot;2&quot;))' 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 either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
  </code></pre>
  <p>If I try precisely the same thing before projecting the entity to a DTO, I have no problems:</p>
  <pre><code>var entities = await query
      .Where(x =&gt; x.Nested.Description.Contains(&quot;2&quot;))
      .ToArrayAsync();
  </code></pre>
  <p>I realize the simple solution is to perform the <code>Where</code> statement before the projection occurs, but that's not an option. The above examples are simplified in order to illustrate the intent. I'm also not interested in evaluating the query client-side just because I don't know a better alternative to the ternary.</p>
  <p>Ideally, I'm just looking for the best practice for conditionally projecting a left join navigation property, such that I can still perform searches on it.</p>
  <p>Edit: I decided to try AutoMapper 10.1.1 and received a similar error:</p>
  <pre><code>var dtos = await query
      .ProjectTo&lt;RootEntityDto&gt;(_mapper.ConfigurationProvider, x =&gt; x.Nested)
      .Where(x =&gt; x.Nested.Description.Contains(&quot;2&quot;))
      .ToArrayAsync();
  </code></pre>
  <pre><code>The LINQ expression 'DbSet&lt;RootEntity&gt;
      .LeftJoin(
          outer: DbSet&lt;NestedEntity&gt;, 
          inner: f =&gt; EF.Property&lt;Nullable&lt;int&gt;&gt;(f, &quot;NestedId&quot;), 
          outerKeySelector: o =&gt; EF.Property&lt;Nullable&lt;int&gt;&gt;(o, &quot;Id&quot;), 
          innerKeySelector: (o, i) =&gt; new TransparentIdentifier&lt;RootEntity, NestedEntity&gt;(
              Outer = o, 
              Inner = i
          ))
      .Where(f =&gt; EF.Property&lt;Nullable&lt;int&gt;&gt;(f.Inner, &quot;Id&quot;) == null ? null : new NestedEntityDto{ 
          Description = f.Inner.Description
      }
      .Description.Contains(&quot;2&quot;))' 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 either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
  </code></pre>