Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

I am Jalpesh Vadgama a Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having more then 6 years of experience in .NET Technology.
Browse by Tags · View All
#ASP.NET 88
#DOTNET 87
BRH 79
ASP.NET 72
#.NET 52
C#.NET 48
DOTNET 47
ASP.NET 4.0 31
ASP.NET MVC 29
VisualStudio 27

Archive · View All
December 2010 16
July 2011 13
April 2011 13
April 2012 12
January 2011 12
June 2011 11
May 2011 11
May 2012 8
February 2013 7
January 2013 7

Deferred vs Immediate execution in Linq

Jun 26 2013 4:08PM by Jalpesh   

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

Tags: dotnet


Jalpesh
15 · 11% · 3548
1
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

"Deferred vs Immediate execution in Linq" rated 5 out of 5 by 1 readers
Deferred vs Immediate execution in Linq , 5.0 out of 5 based on 1 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]