Internal of Deferred or Lazy Execution in LINQ

I have already posted about Deferred Execution here

In this post we will see how internally Deferred or lazy execution is implemented.

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

clip_image001

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

Deferred execution can be avoided

1. By returning single item from the query

2. By converting the query in .ToList().

Lazy evolution of deferred execution is executed using the yield operator in c# 3.0

In c# 3.0 it is implemented as below,

 public static class Sequence
    {
        public static IEnumerable<T> Where<T>(this IEnumerable<T> source,
                                              Func<T, bool> predicate)
        {
            foreach (T element in source)
                if (predicate(element))
                    yield return element;
        }
    }

The main advantage of lazy evolution is if query is returning large result it will cached for better optimization. So here execution is delayed until final query is ready to execute.

About these ads

Fetching lists from SharePoint 2010 site using client object model

Creating the UI

1. Add a label

2. Add a textbox. Give any meaningful name I am leaving default name here.

3. A button. On click event of the button list names will be fetched.

4. A listbox control to bind the lists from SharePoint site.

Setting up the environment

Right click on the project .Click on the Application tab and change the Target framework type to .Net Framework 3.5

clip_image002

Add the references of below dll in the project.

Microsoft.SharePont.Client

Microsoft.SharePoint.Client.Runtime

clip_image004

After adding the dll, add below namespace

clip_image006

Now on the click event of btnGetLists write the below code

Code to Retrieve the Lists of SharePoint site

On click event of the button, write the below code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SharePointclientObj = Microsoft.SharePoint.Client;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnGetLists_Click(object sender, EventArgs e)
        {
             listBox1.Items.Clear();
            //Get a context
             using (SharePointclientObj.ClientContext ctx = new SharePointclientObj.ClientContext(textBox1.Text))
             {
                 //Get the site
                 SharePointclientObj.Web site = ctx.Web;
                 ctx.Load(site);
                 //Get Lists
                 ctx.Load(site.Lists);
                 //Query
                 ctx.ExecuteQuery();
                 //Fill List
                 foreach (SharePointclientObj.List list in site.Lists)
                 {
                     listBox1.Items.Add(list.Title);
                 }

             }
        }
    }
}

Output

clip_image008

Video on Migrating In premise DataBase to SQL Azure