Click here to Skip to main content
6,822,613 members and growing! (19,579 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » LINQ » General     Beginner License: The Code Project Open License (CPOL)

Optimizing LINQ Queries using DataLoadOptions

By Shivprasad koirala

Optimizing LINQ Queries using DataLoadOptions
C# (C#1.0, C#2.0, C#3.0, C#4.0), SQL, .NET (.NET1.0, .NET1.1, .NET2.0, .NET3.0, .NET3.5, .NET4.0), ASP.NET, SQL-Server (SQL2000, SQL-CE, SQL2008), Visual-Studio (VS.NET2003, VS2005, VS2008, VS2010), LINQ, Architect
Revision:3 (See All)
Posted:4 Jul 2009
Updated:5 Jul 2009
Views:7,375
Bookmarked:36 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
15 votes for this article.
Popularity: 5.47 Rating: 4.65 out of 5

1

2
1 vote, 6.7%
3
2 votes, 13.3%
4
12 votes, 80.0%
5

Table of Contents

Introduction

Thanks to everyone who has knowingly / unknowingly helped me to become a Microsoft MVP, hope I can live up to it.

In this section, we will understand the round trip issues of LINQ and how we can overcome the same using ‘DataLoadOptions’. One of the biggest issues with LINQ to SQL is that it fires SQL query for every object which has a huge impact on performance. In this article, we will see how we can get all data in one SQL query.

Catch my videos for WCF, WPF, WWF, LINQ, SilverLight, Design patterns, UML and lot on http://www.questpond.com.

LINQ Basics

This article assumes that you have basic knowledge of how entity objects can be flourished using LINQ. In case you are not aware of basics of LINQ to SQL mapping, you can read my article to understand the basic LINQ concepts.

Customer, Addresses and Phones LINQ Entities

First let’s try to understand how LINQ queries actually work and then we will see how round trips happen. Let’s consider the below database design where we have 3 tables -- customer, addresses and phone. There is one-many relationship between customer and addresses, while there is one-one relationship between address table and phones.

We have created three entities as per the table design:

  • ClsCustomerWithAddresses
  • ClsAddresses
  • ClsPhone

We have defined the relationships between them using ‘EntitySet’ and ‘EntityRef’.

Click to enlarge image

To fill the entity objects with data from table is a 5 step process. As a first step, the datacontext connection is created using the connection string, LINQ query is created and then we start browsing through customer, address and phones.

Click to enlarge image

Analyzing the LINQ SQL Round Trips

Ok, now that we have analyzed that it takes 5 steps to execute a LINQ query, let’s try to figure out on which step the LINQ query actually fires SQL to the database. So what we will do is run the above LINQ code and analyze the same using SQL profiler.

Just so that we do not catch a lot of SQL Server noise, we have only enabled RPC and SQL batch events.

Now when you run the query, you will find the below things:

  • The execution of actual SQL takes place when the foreach statement is iterated on the LINQ objects.
  • The second very stunning thing you will notice is that for every entity, a separate query is fired to SQL Server. For instance, for customer one query is fired and then separate queries for address and phones are fired to flourish the entity object. In other words, there are a lot of round trips.

Click to enlarge image

Avoiding Round Trips using DataLoadOptions

We can instruct LINQ engine to load all the objects using ‘DataLoadOptions’. Below are the steps involved to enable ‘DataLoadOptions’.

The first step is to create the data context class:

DataContext objContext = new DataContext(strConnectionString);

The second step is to create the ‘DataLoadOption’ object:

DataLoadOptions objDataLoadOption = new DataLoadOptions();

Using the LoadWith method, we need to define that we want to load customer with address in one SQL.

objDataLoadOption.LoadWith<clsCustomerWithAddresses>
	(clsCustomerWithAddresses => clsCustomerWithAddresses.Addresses);

Every address object has phone object, so we have also defined saying that the phone objects should be loaded for every address object in one SQL.

objDataLoadOption.LoadWith<clsAddresses>(clsAddresses => clsAddresses.Phone);

Whatever load option you have defined, you need to set the same to the data context object using ‘LoadOptions’ property.

objContext.LoadOptions = objDataLoadOption;

Finally prepare your query:

var MyQuery = from objCustomer in objContext.GetTable<clsCustomerWithAddresses>()
select objCustomer;

Start looping through the objects:

foreach (clsCustomerWithAddresses objCustomer in MyQuery)
{
    Response.Write(objCustomer.CustomerName + "<br>");

    foreach (clsAddresses objAddress in objCustomer.Addresses)
    {
        Response.Write("===Address:- " + objAddress.Address1 + "<br>");
        Response.Write("========Mobile:- " + objAddress.Phone.MobilePhone + "<br>");
        Response.Write("========LandLine:- " + objAddress.Phone.LandLine + "<br>");
    }
}

Below is the complete source code for the same:

DataContext objContext = new DataContext(strConnectionString);
DataLoadOptions objDataLoadOption = new DataLoadOptions();
objDataLoadOption.LoadWith<clsCustomerWithAddresses>
	(clsCustomerWithAddresses => clsCustomerWithAddresses.Addresses);
objDataLoadOption.LoadWith<clsAddresses>(clsAddresses => clsAddresses.Phone);
objContext.LoadOptions = objDataLoadOption;
var MyQuery = from objCustomer in objContext.GetTable<clsCustomerWithAddresses>()
select objCustomer;

foreach (clsCustomerWithAddresses objCustomer in MyQuery)
{
    Response.Write(objCustomer.CustomerName + "<br>");

    foreach (clsAddresses objAddress in objCustomer.Addresses)
    {
        Response.Write("===Address:- " + objAddress.Address1 + "<br>");
        Response.Write("========Mobile:- " + objAddress.Phone.MobilePhone + "<br>");
        Response.Write("========LandLine:- " + objAddress.Phone.LandLine + "<br>");
    }
}

Abracadabra…. Now if you run the code LINQ has executed only one SQL with proper joins as compared to 3 SQL for every object shown previously.

Click to enlarge image

Source Code

We have also attached a source code. Run the project and see how the profiler shows different SQL execution. You can first run the ‘EntitySet’ example and see how SQL profiler reacts for the same and then run the example with ‘DataLoadOptions’. The SQL script is attached in a different file.

))==:- You can the download the source code from here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Shivprasad koirala


Member
I am a Microsoft MVP for ASP/ASP.NEt and currently a CEO of a small E-learning company in India. We are very much active in making training videos , writing books and corporate trainings. You can visit about my organization at http://www.questpond.com and also enjoy the videos uploaded for Design pattern, FPA , UML ,Share Point,WCF,WPF,WWF,LINQ, Project and lot. I am also actively involved in RFC which is a financial open source madei in C#. It has modules like accounting , invoicing , purchase , stocks etc.
Occupation: Architect
Company: http://www.questpond.com
Location: India India

Other popular LINQ articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralCongratulation PinmvpAbhijit Jana12:02 7 Jul '09  
GeneralRe: Congratulation PinmemberShivprasad koirala17:02 7 Jul '09  
GeneralFormatting problems PinmemberHenry Minute6:35 5 Jul '09  
GeneralRe: Formatting problems PinmemberShivprasad koirala8:17 5 Jul '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 5 Jul 2009
Editor: Deeksha Shenoy
Copyright 2009 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2010
Web17 | Advertise on the Code Project