Deferred execution in LINQ

1 minute read

Deferred Execution is used in LINQ, and it means the query is not executed when declaring it; but when calling it.

If you return IQueryable or IEnumerable in your query, the query will not be executed immediately, it will be executed only when you iterate its elements.

But if you return operations on IQueryable or IEnumerable like .ToList(), Count() etc, it will be executed immediately.

Let’s see some examples:

public void DifferedExecutionExample()
{
    int id = 10;
    //differed execution: when query returns IEnumerable or IQueryable
    var query = from c in Context.Customers
                where c.CustomerId > id
                select c;
    id = 20; //this value is finally used because it's the latest value
    Console.WriteLine("Query result:");
    //return customer names whose id is larger than 20
    foreach (var customer in query)
        Console.WriteLine(customer.CustomerName);
}

public void ImmediateExecutionExample()
{
    int id2 = 10;
    //immediate execution: when query doesn't return IEnumerable or IQueryable
    var query2 = (from c in Context.Customers
                    where c.CustomerId > id2
                    select c).ToList();
    id2 = 20; //this value is not used in this case
    Console.WriteLine("Query2 result:");
    //return customer names whose id is large than 10
    foreach (var customer in query2)
        Console.WriteLine(customer.CustomerName);
}

I hope this article can do help to you! Enjoy coding!

SUN Jiangong

SUN Jiangong

A senior .NET engineer, software craftsman. Passionate about new technologies.