Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Entity Framework 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 adding .Include(x => x.Children), the ORDER BY statement that Entity Framework adds to the generated SQL causes the query to take a long time to execute - something like the below:

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

Adding orderby to the linq query doesn't help either, it doesn't affect the statement above other than adding an additional column to sort by.

like image 239
Jamie Avatar asked Aug 26 '14 11:08

Jamie


1 Answers

Apparently, it's something that EF does internally to ease the creation of resulting objects afterwards. You can't remove the order by instruction.

like image 82
Joanvo Avatar answered Oct 20 '22 10:10

Joanvo