I’m looking for confirmation/clarification with these LINQ expressions:
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);
-
Am I correct in thinking the first method is
LINQ to Entitieswhere EF builds a more specific SQL statement to pass on, putting the ordering effort on on the database? -
Is the second method
LINQ to Objectswhere LINQ drags the whole collection into memory (theToList()enumeration?) before ordering thus leaving the burden on the server side (the web server in this case)?If this is the case, I can quickly see situations where L2E would be advantageous (ex. filtering/trimming collections before pulling them into memory).
-
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?
UPDATE:
Let’s say we are not using EntityFramework, this is still true so long as the underlying repository/data source implements IQueryable<T> right? And if it doesn’t both these statements result in LINQ to Objects operations in memory?
You are correct that calling
ToList()forces linq-to-entities to evaluate and return the results as a list. As you suspect, this can have huge performance implications.There are cases where linq-to-entities cannot figure out how to parse what looks like a perfectly simple query (like
Where(x => SomeFunction(x))). In these cases you often have no choice but to callToList()and operate on the collection in memory.In response to your update:
ToList()always forces everything ahead of it to evaluate immediately, as opposed to deferred execution. Take this example:vs
In the second example, any deferred work on
someEnumerablemust be executed before taking the first 10 elements. IfsomeEnumerableis doing something labor intensive (like reading files from the disk usingDirectory.EnumerateFiles()), this could have very real performance implications.