Understanding Deferred Execution

When the CLR encounters a LINQ query, the query is not executed immediately. LINQ queries are executed only when they are effectively used. This concept is known as deferred execution and is part of all LINQ providers that you encounter, both standard and custom ones. For example, consider the query that is an example in the previous discussion:

image

This query is not executed until you effectively use its result. If the query is defined within a function and is the result of the method, the Return instruction causes the query to be executed:

Iterating the result also causes the query to be executed:

Another example ...

Get Visual Basic® 2010 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.