Skip to content

Query: Relational: FirstOrDefault with projection and nav not being fully translated #5085

Closed

Description

@anpete

From MusicStore:

var cartItem 
    = _dbContext.CartItems
        .Select(ci => new CartItem 
            { 
                Count = ci.Count,
                Album = new Album { Title = ci.Album.Title }
            })
        .FirstOrDefault(
            ci => ci.CartId == _shoppingCartId
                && ci.CartItemId == id);

We get the following query plan:

dbug: Microsoft.EntityFrameworkCore.Query.Internal.SqlServerQueryCompilationContextFactory[5]
      TRACKED: False
      (QueryContext queryContext) => IEnumerable<CartItem> _ToSequence(
          element: CartItem FirstOrDefault(
              source: IEnumerable<CartItem> _Select(
                  source: IEnumerable<TransparentIdentifier<ValueBuffer, ValueBuffer>> _Where(
                      source: IEnumerable<TransparentIdentifier<ValueBuffer, ValueBuffer>> _ShapedQuery(
                          queryContext: queryContext,
                          shaperCommandContext: SelectExpression:
                              SELECT [ci].[Count], [ci.Album].[Title]
                              FROM [CartItems] AS [ci]
                              INNER JOIN [Albums] AS [ci.Album] ON [ci].[AlbumId] = [ci.Album].[AlbumId]
                          ,
                          shaper: CompositeShaper`3
                      )
                      ,
                      predicate: (TransparentIdentifier<ValueBuffer, ValueBuffer> t0) => new CartItem{
                          (int) object t0.Outer.get_Item(0)Count = Convert(t0.Outer.get_Item(0)),
                          new Album{ (string) object t0.Inner.get_Item(1)Title = Convert(t0.Inner.get_Item(1)) }
                          Album = new Album() {Title = Convert(t0.Inner.get_Item(1))}
                      }
                      .CartId == string GetParameterValue(
                          queryContext: queryContext,
                          parameterName: ___shoppingCartId_0
                      )
                       && new CartItem{
                          (int) object t0.Outer.get_Item(0)Count = Convert(t0.Outer.get_Item(0)),
                          new Album{ (string) object t0.Inner.get_Item(1)Title = Convert(t0.Inner.get_Item(1)) }
                          Album = new Album() {Title = Convert(t0.Inner.get_Item(1))}
                      }
                      .CartItemId == int GetParameterValue(
                          queryContext: queryContext,
                          parameterName: __id_1
                      )
                  )
                  ,
                  selector: (TransparentIdentifier<ValueBuffer, ValueBuffer> t0) => new CartItem{
                      (int) object t0.Outer.get_Item(0)Count = Convert(t0.Outer.get_Item(0)),
                      new Album{ (string) object t0.Inner.get_Item(1)Title = Convert(t0.Inner.get_Item(1)) }
                      Album = new Album() {Title = Convert(t0.Inner.get_Item(1))}
                  }
              )
          )
      )

This is because we would need to "lift" the FirstOrDefault predicate ahead of the projection (since it is expressed in terms of client objects).

Workaround is to move the predicate to a Where op ahead of the projection.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions