Prevent Entity Framework from adding ORDER BY when using Include

We have a query similar to the following:

from x in db.Table.Include(x => x.Parent) .Include(x => x.Parent.Relation) .Include(x => x.Relation) .Include(x => x.Children) where /* some query */ select x 

The problem is that when you add .Include(x => x.Children) the ORDER BY , which the Entity Framework adds to the generated SQL, causes the query to execute a lot of time - something like below:

 ORDER BY [Project2].[Id1] ASC, [Project2].[Id2] ASC, [Project2].[Id] ASC, [Project2].[C4] ASC 

Adding orderby to the linq query also does not help, it does not affect the above statement, except for adding an additional column for sorting.

+17
source share
1 answer

Apparently, this is what EF does internally to facilitate the creation of resulting objects afterwards. You cannot delete the order by statement.

+4
source

Source: https://habr.com/ru/post/1264505/


All Articles