In this post, We are going to learn about Deferred vs Immediate execution in Linq. There an interesting variations how Linq operators executes and in this post we are going to learn both Deferred execution and immediate execution.
In the Deferred execution query will be executed and evaluated at the time of query variables usage. Let’s take an example to understand Deferred Execution better.
Here in the above code, I have created a customer class and then created a list of customers. After creating a list of customers I have written a query for selecting customers with last name “vadgama”. After writing that query I have added a new customer “Teerth” with “Vadgama” lastName, I have written a query before adding a new customer it should execute immediate but as where operator support deferred execution it will execute when newCustomer used in the code. So output will like following as expected with deferred execution.
So the flow of code will be like below.
In immediate execution query will be executed immediately. No matter whether collection is changed after it or not.
Let’s take example of immediate execution.
Here in the above code I have written almost similar code to the deferred code execution just changed the operator LastOrDefault as it’s not supporting deferred execution and it will immediately execute query. Let’s run example to see how it works.
As expected the “Teerth” customer is the last one as we have added after query.But its displaying “Vishal” as it immediately execute query So flow of the code will be like below.
What is Deferred Execution?
Example:
Following is a code for that.using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var customers = new List<Customer>( new[] { new Customer{FirstName = "Jalpesh",LastName = "Vadgama"}, new Customer{FirstName = "Vishal",LastName = "Vadgama"}, new Customer{FirstName = "Tushar",LastName = "Maru"} } ); var newCustomers = customers.Where(c => c.LastName == "Vadgama"); customers.Add(new Customer {FirstName = "Teerth", LastName = "Vadgama"}); foreach (var c in newCustomers) { Console.WriteLine(c.FirstName); } } public class Customer { public string FirstName { get; set; } public string LastName { get; set; } } } }
Here in the above code, I have created a customer class and then created a list of customers. After creating a list of customers I have written a query for selecting customers with last name “vadgama”. After writing that query I have added a new customer “Teerth” with “Vadgama” lastName, I have written a query before adding a new customer it should execute immediate but as where operator support deferred execution it will execute when newCustomer used in the code. So output will like following as expected with deferred execution.
So the flow of code will be like below.
Linq operators that supports deferred execution
- OrderBy
- OrderByDecending
- Reverse
- Select
- SelectMany
- Skip
- SkipWhile
- Take
- TakeWhile
- Where
What is immediate execution?
Example:
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var customers = new List<Customer>( new[] { new Customer{FirstName = "Jalpesh",LastName = "Vadgama"}, new Customer{FirstName = "Vishal",LastName = "Vadgama"}, new Customer{FirstName = "Tushar",LastName = "Maru"} } ); var newCustomer= customers.LastOrDefault(c => c.LastName == "Vadgama"); customers.Add(new Customer {FirstName = "Teerth", LastName = "Vadgama"}); Console.WriteLine(newCustomer.FirstName); } public class Customer { public string FirstName { get; set; } public string LastName { get; set; } } } }
Here in the above code I have written almost similar code to the deferred code execution just changed the operator LastOrDefault as it’s not supporting deferred execution and it will immediately execute query. Let’s run example to see how it works.
As expected the “Teerth” customer is the last one as we have added after query.But its displaying “Vishal” as it immediately execute query So flow of the code will be like below.
Linq operators that supports immediate execution:
- All
- Any
- Contains
- Count
- First
- FirstOrDefault
- Last
- LastOrDefault
- Max
- Min
- Single
- SingleOrDefault
- Sum
- ToArray
- ToDictionary
- ToList
great article about deferred and immediate execution
ReplyDeleteThanks
Delete