Site Loader
Auckland, New Zealand
In LINQ the query execution has two behavior one is Deferred and another one is Immediate. Deferred is nothing but delayed execution of query whereas immediate execution will execute the script instantly.

Deferred Execution

Consider this example below (Again our same old example which we are dealing in the whole series) Our custom class type (Student)

List of Students

Let’s add one more student after querying the students collection (and ordering them to descending) and see what the result will look while iterating the result as shown below

As you can see above, I am adding a student named Jacob after querying the students collection

Output

Now you might wonder, how this has happened, since we added Jacob only after query execution, where the descending order operation might have already taken place, but still how come the students are correctly ordered based on their StudID, the answer is Deferred Execution behavior of LINQ. Since the execution of LINQ query does not takes place while you do the query, but the actual execution happens while you do Iteration. Hence deferred execution can be better described as
“LINQ Query execution happens only during iteration, rather during query declaration, this execution behavior of LINQ is called as Deferred Execution”
As you can see below, instead of 5 records, LINQ query initially taken 6 (Adding the new value)

Immediate Execution

Here we are going to convert our query to List (remember we already discussed about ToList in Conversion methods in LINQ) Hence our code looks like this

But let’s see what the output

Well wait, what happened to Jacob, do you see the newly added student here, NO. We will see what happened to our student behind the scene by debugging the code.

As you can see from the above code execution, the StudM implicitly typed variable type has got only 5 records, but in the Deferred execution we had 6 records and that’s the reason we couldn’t see our newly added record (Jacob) in immediate execution, whereas the opposite happened in deferred execution.

Purpose

Okay, I got what you are thinking, why do LINQ really has this behavior right? Well, you spot it right. Consider the situation where you might need to store thousand users detail in a list of users collection and you know that only these users will be there for the rest of your code execution while you manipulate them, then instead of having deferred execution, where your LINQ query will every time query the SQL Database for every record during every single iteration, you can use Immediate execution, where it query database only once and store all the details and convert them to List (As done above). This will tremendously increase your performance. But Deferred execution come handy while your data is dynamic and while you need to get the up-to-date data. I hope this article in LINQ series might have given you a complete understanding of the LINQ execution behavior. Please leave your comments and thanks for reading !! Thanks, Karthik KK

Post Author: Karthik kk

One Reply to “Deferred vs Immediate Execution in LINQ (Series)”

Leave a Reply to Surya sasidahr Cancel reply

Your email address will not be published. Required fields are marked *