• A2ZMenu.Com
Loading
  • Home
  • Blogs
    • AJAX
    • ASP.NET
    • BI
    • CSharp
    • JavaScript
    • LINQ
    • Miscellaneous
    • SQL
    • SharePoint
    • Silverlight
    • WCF
  • Tutorials
    • HTML5
    • ASP.NET 4.0
    • CSharp
    • SharePoint
    • SQL Function
  • Online Exam
    • ASP.NET
    • LINQ
    • Silverlight
    • SQL
    • JavaScript
    • SharePoint
  • Forum
  • Utility
  • Fun @ Work
    • Appraisal
    • Funny Images
    • Funny Puzzle
    • Miscellaneous
    • Motivating Stories
  • Wiki
  • Contact Us
  • AJAX
  • ASP.NET
  • BI
  • CSharp
  • JavaScript
  • LINQ
  • Miscellaneous
  • SQL
  • SharePoint
  • Silverlight
  • WCF

Test Your Skill

Show/Hide
 Q. Out of Boxing and Unboxing which one is implicit?
View all questions...
LINQ to SQL DataLoadOptions
Tweet
mail-image

Hello Folks,

Today i will tell tou how to optimize the LINQ query with DataLoadOptions. With a very simple example we will see how easily we can reduce the number of database call.

Here we go...

I have a DBML file that has Customer table and Order table that are mapped by CustomerID.


Now to find out all the Order for a given Customer there exists two ways to write the LINQ query.

Normal non optimized way:

var result = from c indb.Customers

             wherec.CustomerID == "BOLID"

             select c; 

foreach (var item in result)

  {

    foreach(var ord initem.Orders)

     {

       Console.WriteLine(ord.OrderID);

     }

  } 

In this case two query are fired first to fetch all thecustomer for given ID and then a seperate query is fired to fetch all the orderfor given CustomerID.

Optimized Way: 

DataLoadOptions dataload = new DataLoadOptions();

      dataload.LoadWith<Customer>(c => c.Orders);

      db.LoadOptions = dataload;

      varresult = from c indb.Customers

                   wherec.CustomerID == "BOLID"

                   selectc;

 

      db.Log = Console.Out;

      foreach (var item in result)

        {

          foreach(var ord initem.Orders)

            {

              Console.WriteLine(ord.OrderID);

            }

        }

In this case what extra that is  done is that we loaded the Order table in the starting only. This is called the eager loading.

As clear from the query generated this result is just onecall to database. 

This a very simple scenario where you can use the DataLoadOptionsto optimize your query.

Happy Coding J 

Related Posts:

LINQ to SQL Best Practice
Using let Keyword in LINQ
Error: Subquery returned more than 1 value
Maintain Transaction in LINQ to SQL
Error: String must be exactly one character long
Perform Cross Database LINQ join Operation


Add Comment

Comment
Name
 (Required)
E-mail (Privacy assured)
 (Required)

Poll of the Day

  Who is best CEO so far?
 
 
 

Related Contents

Online Test Papers

  • ASP.NET
  • JavaScript
  • LINQ
  • SharePoint
  • Silverlight
  • SQL

Related Tutorials

  • HTML 5 Tutorial
  • SharePoint Best Practice
  • ASP.NET 4.0 Tutorials

Connect with Facebook

Top Blogs

  • SharePoint 2010 Model Dialog
  • Validation of viewstate MAC failed. If this application is hosted...
  • Silverlight: ListBox with CheckBox
  • Creating a Windows Service in C#
  • Using InstallUtil to install/uninstall service
  • How to call webservice using Ajax or JavaScript
  • LINQ to SQL Best Practice
  • The contract name could not be found in the list of contracts implemented by the service
  • Calling JavaScript function from C#
Blogs
  • AJAX
  • ASP.NET
  • BI
  • CSharp
  • HTML
  • JavaScript
  • LINQ
  • SQL Server
  • SharePoint
  • Silverlight
Tutorials
  • HTML5
  • ASP.NET 4.0
  • CSharp
  • SharePoint
  • SQL Server
Online Examinations
  • ASP.NET
  • LINQ
  • SQL Server
  • JavaScript
  • SharePoint
Online Utilities
  • Age Calculator
  • Happy Birthday Wisher
  • Independence Day Wisher
  • URL Encoder Decoder
  •  
Fun @ Work
  • Appraisal
  • Funny Images
  • Funny Puzzle
  • Miscellaneous
  • Motivating Stories
A2ZMenu © 2012 Home | Contact Us | Privacy Policy | Connect with Facebook | Follow us on Twitter