Deferred Query and Non-deferred query

A query is called to be deferred when it is performed during enumeration of the object and Non-deferred query is performed at the time when the query is called.

Example :

Lets say we have an array of integer:
int[] intArray = new int[] { 1,2,3,4,5 };

and we fire  a Linq query on that:
IEnumerable<int> ints = intArray.Select(i => i>2);

//Non-deferred query will be performed here.
// List<int> ints = intArray .Select(i => i>2).ToList();

//Print

//Deferred query will be performed here.
foreach (int item in ints)
{
   Console.WriteLine(” – ” + item + ” – “);
}

 

If by chance I change the value of 0th index of intArray, it will be reflected without again firing Linq query.

Deferred Query,  will be performed during enumeration (when calling foreach) and not when query is executed and  reverse in the case of non-deferred i.e. it will be performed when query gets executed.

Leave a Comment