SIGN UP MEMBER LOGIN:    
ARTICLE

Deferred Execution and Immediate Execution in LINQ

Posted by Dhananjay Kumar Articles | LINQ with C# December 31, 2010
Deferred Execution executes a query only when a loop starts. What I mean here is that, we iterate through the query variable to get the result.
Reader Level:


Deferred Execution executes a query only when a loop starts. What I mean here is that, we iterate through the query variable to get the result.
1.gif

Here the result variable does not contain the actual result. To get the result, we may iterate through the result (query) variable in a loop as many times as we want. We do deferred execution mainly when multiple values are being returned from the query.

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            DataClasses1DataContext context = new DataClasses1DataContext();
            var result = from r in context.Persons select r;
            foreach (var a in result)
            {
                Console.WriteLine(a.FirstName + "" + a.LastName);
            }
            Console.ReadKey(true);
        }
    }
}


Output

2.gif

Immediate Execution happens when LINQ query returns a single value.

3.gif

Above query is returning a single value so it can be executed immediately.

Example

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

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            DataClasses1DataContext context = new DataClasses1DataContext();
            var numberOfRecords = (from r in context.Persons select r).Count();
            Console.WriteLine(numberOfRecords);
            Console.ReadKey(true);
        }
    }
}

Output

4.gif

If we want to make a query returning multiple values or sequence of values as immediate then we need to explicitly convert the result in ToList().

5.gif

So above query is returning multiple values and would be executed immediately.

Example

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

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            DataClasses1DataContext context = new DataClasses1DataContext();

            var result1 = (from r in context.Persons select r).ToList();
            foreach (var a in result1)
            {
                Console.WriteLine(a.FirstName + "" + a.LastName);
            }
            Console.ReadKey(true);
        }
    }
}


Output

6.gif

Login to add your contents and source code to this article
share this article :
post comment
 

I think something that would really help is to either explain how to return a var from a function or that it is not possible. As far as I know, deferred execution is not possible using a var returned from a function.

Posted by Sam Hobbs Dec 31, 2010
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor