Deferred vs Immediate execution in Linq

  • 0
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.

What is Deferred 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.

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.

DeferredExecutioninLinq

So the flow of code will be like below.

FlowinDeferredExecution

Linq operators that supports deferred execution

  • OrderBy
  • OrderByDecending
  • Reverse
  • Select
  • SelectMany
  • Skip
  • SkipWhile
  • Take
  • TakeWhile
  • Where

What is immediate execution?

In immediate execution query will be executed immediately. No matter whether collection is changed after it or not.

Example:

Let’s take example of immediate execution.
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.

ImmgeExecutioninLinq

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.

ImmeDiateExeuctionFlow

Linq operators that supports immediate execution:

  • All
  • Any
  • Contains
  • Count
  • First
  • FirstOrDefault
  • Last
  • LastOrDefault
  • Max
  • Min
  • Single
  • SingleOrDefault
  • Sum
  • ToArray
  • ToDictionary
  • ToList
That’s it. Hope you like it. Stay tuned for more..

Shout it

No comments:

Post a Comment

Your feedback are very important to me. Please provide your feedback via putting comments.