I suspect that the actual underlying reason for the subquery is the anonymous type constructor. Since you do not select a known object, but rather an arbitrary object constructed from other values โโof entities, the EF parser must make sure that it can create an exact set of fields - be it from one table, joined tables, calculated fields, another subqueries, etc. The tree expression parser is very good at writing SQL statements from LINQ queries whenever possible, but it is not omniscient. It processes requests systematically, which will always produce the correct results (in the sense that you will get what you requested), although not always the optimal results.
As for rewriting the query in order to exclude the sub-selection, first turn it off: I see no obvious way to do this, which eliminates the anonymous type and gives the correct results. More importantly, I would not bother . Modern SQL servers such as Sybase are very smart - often smarter than the developer - and very well create the optimal query plan from the query. In addition, EF loves subqueries because they are very good ways to automatically write complex queries. You often find them, even if your LINQ query did not appear. Trying to eliminate them all from your queries will quickly turn into a futile exercise.
Michael edenfield
source share