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.

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

Content Editor Web Part in SharePoint 2010

Target Audience : Begineers

This articles is targeted to beginners of SharePoint 2010.

In this article we will walkthrough

1. Adding a Web part Page

2. Content Editor Web Part on the page.

Follow the steps as below,

Step1

Open SharePoint site and click on Site Action . From Site Action drop down select more option

clip_image002

Step 2

Select Page option and then select Web Part page. And click on Create

clip_image004

Step 3

Give a name of your choice to the page. Select a layout template. Select Share Document as place to save this page and click on Create.

clip_image005

You will get page template as below, if you have selected the same layout as of mine.

clip_image007

Step 4

Click on Add a Web Part and select Media and Content from categories

clip_image009

clip_image011

Step 5

Now in Content Editor type the text.

clip_image013

Step 6

Click on Home page. And from Share Document Library click on MyMediaWebPart page to see the Web part added .

WCF RIA Service Adding Query Methods

Learning WCF RIA Service Part#1 can be read here

This is the second article of learning WCF RIA series. In this article we will see

1. How to pass parameter in query

2. How to return single object

3. How to return multiple objects.

4. How to call the query method with parameter.

In all we will add query methods in this article.

In our previous article, we fetched all the records;

Returning single object

Now let us make our query more particular. If we want to fetch details of a particular Person of given PersonID then we need to modify. So to modify the query open DomainService1.cs and modify the query as below,

 

clip_image002

1. We pass the parameter Id and returning the Person objects.

2. If query is retuning single object then we will have to make [Query (IsComposable=false)].

Now to call this service we need to call the method as below,

MainPage.xaml.cs

using System.Windows.Controls;
using RIA1.Web;
using System.ServiceModel.DomainServices.Client; 
 
namespace RIA1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            DomainService1 context = new DomainService1();
            LoadOperation<Person> loapPerson = context.Load
                                                (context.GetPersonByIdQuery(1));          
            myGrid.ItemsSource = loapPerson.Entities; 
 
        }
    }
}
 
 
 

 

In above we are passing person id as 1.

Retuning List of objects

Now if we want to pass a parameter and expecting list of objects to return, then we need to modify DomainService1.cs as below,

 

clip_image004

And we will call above method as below,

MainPage.xaml.cs

using System.Windows.Controls;
using RIA1.Web;
using System.ServiceModel.DomainServices.Client; 
 
namespace RIA1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            DomainService1 context = new DomainService1();        
            LoadOperation<Person> loapPerson = context.Load
                               (context.GetPersonsbyStartNameQuery("b"));
            myGrid.ItemsSource = loapPerson.Entities; 
 
        }
    }
}
 
 
 


			

Migrating Data Base from SQL Server 2008 R2 Enterprise to SQL Azure

In this walkthrough we will migrate an existing Database from SQL Server 2008 R2 to SQL Azure.

Creating Database

The first step is to create database. We are going to use School database. Script of sample School Database copy from here

Generate Database Script for SQL Azure

Right click on School Database and select Tasks. From Tasks select Generate Script.

clip_image001

From Pop up select Set Scripting option.

clip_image003

Give the file name by selecting Save to file option.

Now the main thing to be noticed here is we need to so some change in advanced setting. For that click on Advanced options.

clip_image004

After that click next and Finish. You can see a SQL file is created and we will be using this script to migrate our in house school database to SQL Azure.

Create School Database in SQL Azure

Login SQL Azure portal with your live credential

https://sql.azure.com/

Click on SQL Azure tab. You will get the project , you have created for yourself.

clip_image006

Click on the project. In my case project name is debugmode. After clicking on project, you will get listed the entire database created in your SQL Azure account.

clip_image008

Here in my account there are two database already created. They are master and student database. Master database is default database created by SQL Azure for you.

Click on Create Database

clip_image010

Give the name of your database. Select the edition as Web and specify the max size of database.

clip_image011

You can select other option also for the edition as business

After that click on Create you can see on Databases tab that Demo1 database has been created.

clip_image013

Run the Script in SQL Azure

Open SQL Server management studio

clip_image014

You will get Connect to server dialog box. Click cancel on that.

clip_image015

After cancelling the dialog box click on New Query from left top

clip_image016

On clicking New Query, you will get the connect to server dialog box again.

clip_image017

Now here you need to provide, Server name of SQL Azure and Login credential of SQL Azure.

To know what is database server name of SQL Azure portal, login to Windows Azure portal with your live credential and then click on SQL Azure tab

clip_image019

You will get the server name in form of

abc.database.windows.net

Where abc is name of your SQL Azure server. We need to provide this server name at local sql server management studio.

clip_image020

Make sure to select SQL Server Authentication and provide login user name and password of your SQL Azure database portal.

After that before clicking Connect click on Option

clip_image021

From Option select School database.

clip_image022

Run the Script

Now once you successfully got connected to School Database in SQL Azure. Copy the script and Run like below.

clip_image024

After successfully running of script , run the below command and all the tables name will get listed.

clip_image025

In this way you successfully migrated database to SQL AZURE.

Tracking Change Data Capture in SQL Server 2008

A very important feature of SQL Server 2008 is that we can enable CDC [Change Data capture] on database or table.

We can track the database had CDC enabled by querying IS_CDC_ENABLED column

clip_image001

In above query School is name of the database.

If we want to track tables in database whether CDC enabled or not then

clip_image002

Above query will list name of all tables in School database CDC enabled.