LINQ to Entities -- OrderBy().ToLisT() vs. ToList().OrderBy()

entity-framework linq linq-to-entities performance


The following LINQ expressions need confirmation or clarification:

var context = new SomeCustomDbContext()

// LINQ to Entities?
var items  = context.CustomItems.OrderBy(i => i.Property).ToList();

// LINQ to Objects?
var items2 = context.CustomItems.ToList().OrderBy(i => i.Property);

(Q1) Do I have this right that the first approach isLINQ to Entities where EF constructs a more precise SQL statement to transmit, placing the database's ordering effort?

(Q2) The second approach isLINQ to Objects LINQ drags the entire collection into memory in this case (theToList() before placing an order, leaving the workload on the server side (in this case, the web server)?

If so, I can immediately think of circumstances where L2E might be useful, such as when filtering or pruning collections before bringing them into memory.

(Q3) But are there any other considerations or trade-offs I need to be aware of, or are there any circumstances in which "way 2" could be preferable to the first technique?


Assuming that we don't use EntityFramework, as long as the underlying repository or data source supportsIQueryable<T> right? And if it doesn't, both of these claims lead toLINQ to Objects activities in memory?

12/11/2012 7:39:51 PM

Accepted Answer

  1. Yes.
  2. Yes.
  3. Yes.

That's accurate, callingToList() makes linq-to-entities perform the evaluation and return the information as a list. This may significantly affect performance, as you could have guessed.

In some circumstances, linq-to-entities is unable to understand what appears to be an incredibly straightforward query (such asWhere(x => SomeFunction(x)) You frequently have no choice but to call in these circumstances.ToList() and perform operations on the memory collection.

Regarding your update:

ToList() Always enforces instant evaluation rather than delayed execution on everything before it. Use this illustration:




In the second illustration, any postponed work onsomeEnumerable must be carried out prior to selecting the first 10 items. IfsomeEnumerable When performing a time-consuming task (such as reading files from a disk utilizingDirectory.EnumerateFiles() ), this might have extremely noticeable effects on performance.

12/11/2012 7:49:15 PM

Popular Answer

Am I correct in thinking the first method is LINQ to Entities where EF builds a more specific SQL statement to pass on, putting the ordering effort on on the database?


Is the second method LINQ to Objects where LINQ drags the whole collection into memory ... before ordering thus leaving the burden on the server side ...?


But are there any other details/trade-offs I should be aware of, or times when "method 2" might be advantageous over the first method?

Method 1 will frequently not work, mainly when you have a complicated filter or sort order that can't be converted into SQL (or more appropriately where EF does not support a direct SQL translation). Considering that you cannot transport lazy-loadedIQueryable Every time a result needs to be serialized over-the-wire, you'll need to materialize it first withToList() or an equivalent.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow