LINQ Deferred Execution

This is showing a simple example of deferred execution on a lazy LINQ query operator. Basically I’ll be showing that data will still show up after the execution of the LINQ query.

DeferredExecutionProgram DeferredExecutionSolution

 

Program.cs Code Contents

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;

namespace MyLinq
{
    class Program
    {
        /// <summary>
        /// Demonstrating an example of deferred execution on a LINQ query.
        /// </summary>
        /// <param name="args"></param>

        static void Main(string[] args)
        {
            QueryEmployee();
            QueryTypes();

            Console.ReadLine();
        }

        //Private Methods
        //
        private static void QueryEmployee()
        {
            Console.WriteLine("****Demonstrating Deferred Execution****");

            // Creating am IEnumerable list of Employee type that I can use LINQ on.
            List<Employee> employees = new List<Employee>()
            {
                new Employee { ID = 1, Name = "Daniel", HireDate = new DateTime(2002, 3, 5) },
                new Employee { ID = 2, Name = "Sara", HireDate = new DateTime(2002, 10, 15) },
                new Employee { ID = 3, Name = "Jack", HireDate = new DateTime(2007, 10, 11) },
                new Employee { ID = 4, Name = "Robert", HireDate = new DateTime(2010, 4, 22) }
            };

            // Creating a LINQ query with lazy operators.
            IEnumerable<Employee> query =
                from e in employees
                where e.HireDate.Year < 2005
                orderby e.Name
                select e;

            // Adding a new employee to the list after initialization to show that when the collection is iterated
            // thru this employee object will show.
            employees.Add(new Employee { ID = 5, Name = "Paul", HireDate = new DateTime(2002, 2, 10) });

            // Displaying the results of the query.
            foreach (Employee e in query)
            {
                Console.WriteLine(e.Name);
            }
            Console.WriteLine();
        }

        private static void QueryTypes()
        {
            Console.WriteLine("****LINQ Execution On Public Object Types****");

            // Creating the IEnumerable list for the LINQ query.
            IEnumerable<string> publicTypes =
                from t in Assembly.GetExecutingAssembly().GetTypes()
                where t.IsPublic
                select t.FullName;

            foreach (string name in publicTypes)
            {
                Console.WriteLine(name);
            }
            Console.WriteLine();
        }
    }
}

First thing you notice is I’m creating Employee objects like I did in the other example. The code is the same. So refer to that if you need to. The class was very simple with only a few properties.

In the QueryEmployee method you will see that I’m creating a List of Employee objects to run my LINQ query on. Then after the query I’m adding one more Employee object. You will notice that once the foreach loop runs the added Employee object shows up. This is showing a simple example of deferred execution on a lazy LINQ operators and the evaluation of the expression is delayed until the realized value is required. In this case it’s required when the foreach loop runs. Basically the operator runs at runtime instead of compile time.

By doing this on a larger scale you can increase the performance of an application if you are manipulating large amounts of data or chaining queries.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s