Increase Query Performance with Read-Only Data in LINQ

No.of Views1984
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  10 Jan 2011 10:01:52
Tag : LINQ , Application Performance Optimization
By default CRUD operation can be performed on the retrieved data from LINQ query. We can modify the data fetched through LINQ query.IF we do not want to modify the data then we can increase the performance by making the data as READ ONLY.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

By default CRUD operation can be performed on the retrieved data from LINQ query. We can modify the data fetched through LINQ query.IF we do not want to modify the data then we can increase the performance by making the data as READ ONLY

Image Loading

If we set ObjectTrackingEnabled as false for DataContext then framework will not track the changes done on the DataContext. 

Program.cs

using System;
using System.Linq;
using System.Data.Linq;
namespace ConsoleApplication5
  {
    class Program
        {
            static void Main(string[] args)
                {
                    DataClasses1DataContext context = new DataClasses1DataContext();
                    context.ObjectTrackingEnabled = false;
                    var result = from r in context.Persons orderby r.FirstName select r;
                    foreach (var r in result)
                    {
                        Console.WriteLine(r.FirstName + " " + r.LastName);
                    }
                    Console.ReadKey(true);
                   
                    
                }
        }
}

Output 

Image Loading

There are two scenarios while ObjectTrackingEnabled as false can throw exception

1.    If we execute the query and after that making the ObjectTrackingEnabled as false. 

Image Loading

Then we will get below exception

Image Loading

2.    If we try to perform submitchanges() operation because DataContext is on readonly.

That's all. happy coding.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
By:vtimashkovDate Of Posted:1/11/2011 10:09:37 AM
vtimashkov
Thanks for this tip! The only little inaccuracy - it is not about LINQ itself, it is about an ORM provider - LINQ-to-SQL.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top
</