Click here to Skip to main content
16,009,407 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I happened to asked a question in interview that what is difference between OrderBy().ToLisT() and ToList().OrderBy()? I replied there is performance overhead using first one as ToList forces everything to be evaluated. But he didn't' agree with my argument and asked me to explain comprehensively. Can you please help me to understand these in depth ?

Thanks in advance
Posted
Comments
PIEBALDconsult 21-Sep-15 17:36pm    
Both should have ToList removed from them.

1 solution

You were right about the performance thing (at the global level at least)...
The reason that the OrderBy().ToList() version should perform better is that LINQ to Entities will translate that OrderBy() to an SQL ORDER BY statement that perform much better than any sorting method...
It is not always true as the expression for order may be too complex to pass it to SQL in which case LINQ to Entities will evaluate data first anyway...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900