Skip to content

Query: Query containing subquery with annonymous type fails to translate to server #7844

Closed

Description

@smitpatel

With #7843 We introduced optimization to lift subqueries when outer QM has selector only and it can be pushed down into subquery model.
Query

AssertQuery<Employee, Order>((es, os) =>
    from e in es.Take(9).Select(e => new { e })
    from o in os.Take(1000).Select(o => new { o })
    where e.e.EmployeeID == o.o.EmployeeID
    select new { e, o });

generates query model from parser like this

  Compiling query model: 
  'from Employee e in 
      (from Employee <generated>_1 in DbSet<Employee>
      select <generated>_1)
      .Take(__p_0)
  from <>f__AnonymousType15<Order> o in 
      from Order o in 
          (from Order <generated>_1 in DbSet<Order>
          select <generated>_1)
          .Take(1000)
      select new <>f__AnonymousType15<Order>(o)
  where (Nullable<int>) e.EmployeeID == o.o.EmployeeID
  select new <>f__AnonymousType14<<>f__AnonymousType13<Employee>, <>f__AnonymousType15<Order>>(
      new <>f__AnonymousType13<Employee>(e), 
      o
  )'

which is being optimized into this now

  'from Employee e in 
      (from Employee <generated>_1 in DbSet<Employee>
      select <generated>_1)
      .Take(__p_0)
  from <>f__AnonymousType15<Order> o in 
      (from Order o in DbSet<Order>
      select new <>f__AnonymousType15<Order>(o))
      .Take(1000)
  where (Nullable<int>) e.EmployeeID == o.o.EmployeeID
  select new <>f__AnonymousType14<<>f__AnonymousType13<Employee>, <>f__AnonymousType15<Order>>(
      new <>f__AnonymousType13<Employee>(e), 
      o
  )'

Since the AdditionalFromClause has anonymous type projection, we fail to translate the whole query as cross join.

The root cause here seems to be QueryParser. Even though MainFromClause & AdditionalFromClause both have same structure, the where predicate for MainFromClause is optimized from new { e }.e.EmployeeID to e.EmployeeId. While same optimization does not happen for AdditionalFromClause.

Activity

  1. smitpatel commented on Mar 10, 2017

    @smitpatel
    ContributorAuthor

    @anpete - Should we report this to Re-Linq or try to work-around on our side?

  2. tuespetre commented on Mar 10, 2017

    @tuespetre
    Contributor

    @smitpatel @anpete this is not a problem with Re-Linq. It's just that once you moved the selector down into the subquery, the subquery now gets flagged for RequiresClientProjection unnecessarily. I just locally cherry-picked #7843 on top of my other PR #7767 and it is resolved.

    See: tuespetre@4933293

  3. smitpatel commented on Mar 10, 2017

    @smitpatel
    ContributorAuthor

    @tuespetre - Good to know that #7767 fixes this. I did not mean that this is issue in Re-Linq. Though certain an optimization which Re-Linq can expand. 3 components - ReLinq QueryParser, EF QM optimizations & EF Query translations are involved in this translation. So we can apply fix at any place. It is the same difference that instead of lifting subqueries in translation we lifted in QueryOptimizer. If Re-Linq generates more optimized QM then its good for us. If not then we improve translation logic to deal with such cases. (Exactly what you are doing in #7767. Thanks.)

    Just wanted to make sure that we have issue tracking the test that we should do something to enable translation to Server.

  4. added a commit that references this issue on Mar 11, 2017
    3f2c3fd
  5. added this to the 2.0.0 milestone on Mar 13, 2017
  6. added 3 commits that reference this issue on Mar 15, 2017
    6ce36d4
    f654e1b
    4442d1a
  7. changed the title [-]Query: Test Where_subquery_anon fails to translate to server[/-] [+]Query: Query containing subquery with annonymous type fails to translate to server[/+] on May 10, 2017
  8. modified the milestones: , 2.0.0 on Oct 15, 2022
  9. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions