Technology News

April 13, 2011

Differed Execution (vs) Immediate execution in LINQ

Filed under: 2011, April, C# 3.0, Code, LINQ, Tips, Wednesday — చక్రవర్తి @ 8:56 pm

Recently while understanding LINQ, many things unfurled from MSDN. The main theme of .NET is lazy binding (or) lazy loading. And it is evolving in all the ways within the .net Framework. I recollect in one of my previous posts about the mainly concentrated theme for every version that is released. I could be wrong from the insider perspective, but what I’ve observed is what I’ve written.

Anyhow, coming back to the main purpose of this post, which is all about the lazy loading and the point behind various execution mechanisms that are available within LINQ. Before I proceed further, one point is very certain that, choosing LINQ is costlier from the point of “Response Time” from DB to any connected system. This statement is true when compared the LINQ with any traditional mechanism of database connectivity.

Traditionally, every developer writes code with Connection object for connecting to the Data Store and then disposes the connection. This method can be by various objects that are provided within .net framework. One of the mechanism is that

  • Use a connection object and DataAdapter
  • Use a connection object along with Command and use either DataReader or DataSet

These mechanisms are pretty neat and straight, while connecting to the datastore. Where as they don’t provide the query mechanism to the developer against the returned results from the datastore. Ofcourse, you still could do the “Filter” method for the dataset or datatable object. This again doesn’t give the generic way to query from the data in the dataset / datatable.

LINQ, in this context provides a great flexibility for the developer to query against the data schema and provides a decent means of all kinds of DataBase querying techniques. In other words, I could say that LINQ gives the power of Database Querying to the C# developer. If your applications are developed using LINQ, you don’t need to have a DB developer. But you need the DB administrator to understand the health of the database and fine tune the queries used by LINQ. Hence, by choosing LINQ for your projects you are saving cost to the project owner. So, To LINQ or NOT to is upto you to decide. Let me not write more here about  LINQ and jump to the execution mechanisms within LINQ.

As titled in the post, there are 2 mechanisms for the execution of LINQ Results, I say it is about LINQ RESULTS. They are differed execution as well as Immediate execution. The main difference between these two are the lazy loading (or) JustInTime loading at the run time. To make simple this entire story, let me give you some code snippet, so that you would understand. For this am trying to query the Northwind Database.

Requirement:

Display all the employee names along with the product names and total of sold quantity

 

Solution 1 : In this solution am trying to use the LINQ as an Expression.

var empCounts = from eachEmpData in
 (from eachSale in
   (from eachEmp in nwDB.Employees
      join eachOrder in nwDB.Orders on eachEmp.EmployeeID equals eachOrder.EmployeeID
      join eachOrderDetails in nwDB.Order_Details on eachOrder.OrderID equals eachOrderDetails.OrderID
     join eachProduct in nwDB.Products on eachOrderDetails.ProductID equals eachProduct.ProductID
     where eachOrder.OrderDate > dtStart && eachOrder.OrderDate < dtEnd
                          select new
                          {
                              FirstName = eachEmp.FirstName,
                              LastName = eachEmp.LastName,
                              ProductName = eachProduct.ProductName,
                              OrderQuantity = eachOrderDetails.Quantity
                          }
                        )
    group eachSale by new { eachSale.FirstName, eachSale.LastName, eachSale.ProductName } into groupSales
                     select new
                     {
                         groupSales.Key.FirstName,
                         groupSales.Key.LastName,
                         groupSales.Key.ProductName,
                         Sold = groupSales.Sum(qty => qty.OrderQuantity)
                     })
                select eachEmpData;
 
// the below line binds the data from LINQ Expression

bsLINQData.DataSource = empCounts;

In this solution, you could see that I’ve written all the joins similar to that of the Database SQL joins. If you are familiar with all such queries, then it is very easy to write. But this kind of writing is also expensive. The alternative for such queries is the next solution.

Solution 2: In this solution am trying to use the same above LINQ as methods

var empCounts = nwDB.Orders
    .Where(eachOrder => eachOrder.OrderDate > dtStart && eachOrder.OrderDate < dtEnd)
                    .SelectMany(eachOrderDetails => eachOrderDetails.Order_Details)
                    .GroupBy(od => new { od.Product, od.Order.Employee })
                    .Select(od => new
                    {
                        FirstName = od.Key.Employee.FirstName,
                        LastName = od.Key.Employee.LastName,
                        ProductName = od.Key.Product.ProductName,
                        Sold = od.Sum(q => q.Quantity)
                    })
                    .OrderBy(s => s.Sold);

                // the below line binds the data from LINQ Expression
                bsLINQData.DataSource = empCounts;

In this solution, you find less coding and much usage of LINQ as methods. This kind of coding is much better than the previous solution. For the records, the above solution has taken about 3 milliseconds on my laptop where as the current solution took about 1 millisecond to query. See below for the stats and comparison.

Solution 3: The above two solutions are Differed execution, where as the current one is all about Immediate execution.

var empCounts = nwDB.Orders
    .Where(eachOrder => eachOrder.OrderDate > dtStart && eachOrder.OrderDate < dtEnd)
    .SelectMany(eachOrderDetails => eachOrderDetails.Order_Details)
    .GroupBy(od => new { od.Product, od.Order.Employee })
    .Select(od => new
    {
        FirstName = od.Key.Employee.FirstName,
        LastName = od.Key.Employee.LastName,
        ProductName = od.Key.Product.ProductName,
        Sold = od.Sum(q => q.Quantity)
    })
    .OrderBy(s => s.Sold).ToList();

// the below line binds the data from LINQ Expression
bsLINQData.DataSource = empCounts;

Here you have to observe the query ended with .ToList() and that is the trick of this query. When you see the statistics, it is very surprising that the time taken to query is very negligible when compared with binding the query results to the binding source. Which means that it is evident to say, LINQ doesn’t fetch the data when it is queried, but it fetches data when it is actually binded to any source.

Finally, the results when compared the above all the three solutions are as mentioned below

  Query Bind
Expression 3 60
Methods 1 44
InList 35 9

 

Conclusion :

Solution 1 and Solution 2 are the examples for Differed Execution, where as the 3rd solution is an example for Immediate execution. Any comments?

January 25, 2011

ASP.NET 70-515 Exam Preparation points

Filed under: 2011, 25, ASP.NET, Certificates, January, Tips, Tuesday — చక్రవర్తి @ 7:27 pm

I’ve written the mentioned exam and completed this in the second attempt. I’ve failed in my first attempt, but passed in the second attempt. The second attempt for all the Microsoft exams is free. One of my friend asked me about the highlight points that helped me to attend the exam. This post arrived while preparation of the bulleted points that I recollect from the exam.

The whole exam pattern can be divided into 3 + 2 = 5 sections. The first 3 sections are more concentrate on the enhancements in v4.0 along with the traditional questions from v3.0 based. The second 2 sections are mainly into configuration and deployment of any web app that is developed using ASP.NET v4.0

To make the long story short, here is the full details that I’ve in my mind. Ofcourse, I’ve not mentioned any thing about MVC as a separate section, because, MVC is a pattern for me but not a new enhancement from the point of framework. Having mentioned that you can even consider that there are few questions pertaining about MVC too, thus, don’t forget to give attention towards MVC.

1) Traditional WebApplication questions

1) How to Configure Web Forms pages 
2) What are master pages and themes 
3) What is globalization

4) What are the page life cycle events

5) How to implement caching and how to manage view-state & Validate user input

2) Questions that are new to ASP.NET v4.0 Controls

6) What are user controls and how to implement 
7) What are the mechanisms using which we can manipulate user interface controls from code-behind

8) What controls help to Display and Manipulate Data 
9) What are the new ways to implement DataBound / DataSource controls

10) Query and manipulate data by using LINQ (ofcourse, this doesn’t new to v4.0 but the exam questions are based on v3.0 LINQ)

3) This section is about new enhancements from ASP.NET v4.0

11) What is Client-Side Scripting and AJAX 
12) How to induce the dynamic features to a page by using JavaScript 
13) How to handle JavaScript events

3.1)Services Enhancements

14) How to create and consume a data service 
15) How to create and configure a Dynamic Data project

4) Configuration Section targeting the production environment

16) How to configuring and Extending a Web Application 
17) How to configure authentication and authorization 
18) How to configure providers 
19) How to create and configure HttpHandlers and HttpModules 
20) How to Configure initialization and error handling 
21) How to reference and configure ASMX and WCF services 
22) How to Configure projects and solutions, and reference assemblies 
23) What are custom routes and how to create controllers & actions

5) Standard Deployment of WebApps

24) How to debug a Web application 
25) How to deploy a Web application

So, if you are attending this exam, please give your self a second study about the above mentioned points. If you are aware of fundamentals of these topics, that’s it you are thru. Happy certification.

April 7, 2010

SVN Repository Migration

Filed under: 2010, 7, April, bing, SVN, Tips, Wednesday — చక్రవర్తి @ 8:14 pm

Our current employer decided to have the code base as VisualSVN. So we stated a demo server and then after successful trail started a staging server for our project. For the entire March month it worked well, and we decided to have another VisualSVN server as production.  During the last week we have decided to migrate our staging SVN source to production box, but now the problem is about history or revision of the full one month.

After binging and googling, I’ve found many alternatives. Most of all, I’ve realized one simple solution and migrated the entire code base from staging to production as simple as cake biting.

Problem :

Source SVN is at 10.4.5.68 and the repositories are installed at D:\

Target SVN is at 119.18.112.63 and the repositories are installed at E:\

Solution:

Step 1:

Copy all the project folders from 5.68\d$\repository\<<projectRepositories>> to 112.63\e$\repository\

Step 2:

Copy the auth files from 5.68 to 112.63, this would copy all the users along with their project groups

Step 3:

Go to the client side and just relocate your SVN url by right clicking on the folder and point to “SVN=>Relocate”

That’s is done.. did you try?

March 6, 2008

IE 8 Beta1 and SL2.0 – Today

Filed under: ASP.NET, Blogroll, Expression, IE, Microsoft Promotion, Silverlight, Tips, v3.5, VS2008 — చక్రవర్తి @ 9:33 pm

image

Today, I’ve downloaded IE8 beta 1 as well as Silverlight 2.0 installed on my laptop. This day have seen many products released at Mix 08 by great personalities like Scott GuthrieBob Familiar, Guy Burstein, IEBlog, etc. According to Jane Kim at an interview for Mix08, Activities and WebSlices are two of the most exciting new features in IE8.

image

Apart of IE8.0 and SL2.0, there are other products such as

I’ve downloaded all these .. and am trying out one by one .. what about you??

February 19, 2008

Increase Web Pages Response time – tip

Filed under: ASP.NET, Tips — చక్రవర్తి @ 12:04 pm

For the ASP.NET pages that has no user input it is a best practice to have the page register with EnableViewState=“false”. This will increase the response time to 200% of the default.

Along with that, you can also do the one more important is that, anyhow these pages have no user input. Secondly, these will have data that is read-only from database. Thus, you are sure that there is no activity from the user that could be saved onto disk. Perhaps the user might work with the data that is available on the page.

Hence, keeping all the above point in mind, by setting the session’s state to read-only makes the WebPages more effective and the response time increases drastically to about another 200%. Think this and change your web page attributes from the next time onwards

del.icio.us Tags: , , ,

Technorati Tags: , ,

December 17, 2007

VS 2008 and .NET 3.5 Training Kit

Filed under: Design Patterns, Microsoft Promotion, Silverlight, Tips — చక్రవర్తి @ 2:28 pm

The Visual Studio 2008 and .NET Framework 3.5 Training Kit includes presentations, hands-on labs, and demos. This content is designed to help you learn how to utilize the Visual Studio 2008 features and a variety of framework technologies including: LINQ, C# 3.0, Visual Basic 9, WCF, WF, WPF, ASP.NET AJAX, VSTO, CardSpace, SilverLight, Mobile and Application Lifecycle Management.

I’ve downloaded this, if any one require this, you can drop me a mail or can collect from me.

Microsoft Products at No Cost – Free

Filed under: Microsoft Promotion, Tips — చక్రవర్తి @ 10:53 am

Who says that Microsoft manufactures products only to sell ?? Microsoft has many products that are free to download. As am into .NET and specially with Visual Studio, am aware that Visual Studio is free to download from Microsoft’s website. Yes, that’s true.. Microsoft give Visual studio 2008 Express Editions, MSDN Library for Visual Studio 2008 as well as the Runtime for the latest .NET framework 3.5.

Here is the link to learn more towards free products as well as priced products. To know more about the product information about Visual Studio 2008, click here.

December 11, 2007

Laptop under $100

Filed under: First, Microsoft Promotion, Tips — చక్రవర్తి @ 5:47 pm

That’s true, you read the title correct. BBC recently wrote about this on 6th Dec 08. Microsoft is working out towards the XP operating system to be a reality on the co called $100 laptop or XO. If things go as scheduled, $100 Laptop or XO will be a reality by the mid 2008.

James Utzschneider, GM for Marketing and Communications from Microsoft, wrote about this at his blog. According to him, about 40 engineers are working towards mid 2008 to come out with a production-quality release. The XO laptop works with 1GB Flash memory, when compared with traditional Hard Disk Drive.

OLPC, One Laptop Per Child, organisation is having a great idea towards making this product into a reality. During June 2008, Intel announced a laptop under $220 with a title as “Classmate PC“. This is similar to that of XO laptop, but some of the basic difference is kind of 2GB Flash memory in ClassmatePC, where as 1GB in XO laptop.

These kind of laptops are really good towards educating the next generation kids towards the technology. Isn’t a great work..

Servers at Home

Filed under: Microsoft Promotion, Silverlight, Tips — చక్రవర్తి @ 3:07 pm

Servers belong to Office. All these days, wife’s worry that their husband not only work wholly with LapTop, on top of this, Micorosoft is coming out with one more product titling as “Stay at Home Servers“. Don’t you believe this .. pay a visit to their product page. Microsoft addresses this as “WindowsHomeServer“. The advantages explained in nice video. This video has all the possible places that every home can have and what every one can do from every location of common home.

Where are we going .. and where is the infrastructure is moving to ?? anyhow, when will India have all such infrastructure inbuilt apartments or homes built with such facilities.

December 10, 2007

ASP.NET 3.5 Extensions Preview

Filed under: ADO.NET, ASP.NET, Microsoft Promotion, Silverlight, Tips, v3.5 — చక్రవర్తి @ 8:03 pm

The ASP.NET 3.5 Extensions Preview provides a glimpse of new, powerful functionality being added to ASP.NET 3.5 and ADO.NET next year. This release delivers features that enable high-productivity data scenarios and creates the best server for rich clients. The release includes an ASP.NET model-view-controller (MVC) framework, ASP.NET Dynamic Data, Silverlight controls for ASP.NET, ADO.NET Data Services, an Entity Framework runtime, new features for ASP.NET AJAX and a wide variety of API References as hosted at ASP.NET Official site.

You can download the ASP.NET 3.5 Preview, which contain all the Siliverlight controls, ADO.NET Data Services and many more. There is a forum specially dedicated to ASP.NET 3.5 Preview at ASP.NET Forums.

Older Posts »

Create a free website or blog at WordPress.com.