Geeks With Blogs
Developers Friendly

LINQ (Linear Integrated Query) in used to query the datasource against some specific condition. The main purpose of using this LINQ is to avoid the process of visiting each object and check condition and perform operation, which is costlier and time consuming.

Below is the example query.

LINQ Query to fetch male

LINQ query execution will take place in two way,

  • Deferred Execution
  • Immediate Execution


Deferred Execution:

In Deferred execution, the execution will take start when we start using “peopleList” (See above sample query). When control enters foreach loop then LINQ query will be executed.


Immediate Execution:

In Immediate execution, the execution will take place along with LINQ query, this is when we use any aggregation methods (like ToList(), Count(), ToArray()).


In the above query we are converting output of LINQ query to List, hence here query will be executed immediately.

For better understanding, i am pasting sample program, hope it helps.

Note: Debugging the below program will help in better understanding the concept.

using System;
using System.Collections.Generic;
using System.Linq;

namespace DefferedAndImmediateExecution
    class Program
        static void Main(string[] args)
            int numOfPpl = 0;

            Console.Write("Enter number of people need to be created:");
            numOfPpl = int.Parse(Console.ReadLine());

            //Creating array of person
            Person[] people = new Person[numOfPpl];

            for (int i = 0; i < numOfPpl; i++)
                //Creating object
                people[i] = new Person();

                people[i].Name = "Person" + i.ToString();
                if (i % 2 == 0)
                    people[i].Gender = 'M';
                    people[i].Gender = 'F';

            //LINQ query to fetch all male persons
            var peopleList = from person in people
                             where person.Gender == 'M'
                             select person;

            //Immediate Execution
            List<Person> pplLst = (from person in people
                                   where person.Gender == 'M'
                                   select person).ToList();

            //Query to fetch all male persons using lambda expression
            //var peopleList = people.Where(person => person.Gender == 'M');

            //Immediate Execution
            Console.WriteLine("\nNumber of persons created: " + people.Count().ToString());

            Console.WriteLine("\nList of Male persons\n");
            //Deffered Execution
            foreach (var person in peopleList)
                Console.WriteLine("Person Name: " + person.Name);

    class Person
        string name;

        public string Name
            get { return name; }
            set { name = value; }

        char gender;

        public char Gender
            get { return gender; }
            set { gender = value; }




Posted on Sunday, August 4, 2013 8:20 AM .NET , LINQ | Back to top

Comments on this post: Deferred and Immediate Execution in LINQ

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Manjunath K | Powered by: