Increase Query Performance with Read-Only Data in LINQ

No.of Views1956
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
    Now a days most of the developers are moving towards new LINQ to SQL they find difficult to write down SQL query in C# to query data using LINQ. LINQ is a query language which get integrated in C# to query data form ObjectCollects, SQL, XML etc.
    Published Date : 08/Jan/2011
    In this article I will explain how to improve LINQ to SQL query performance with Complied Query. There may be scenario where we need to execute a particular query many times and repeatedly. LINQ allows us to make this task very easy by enabling us to create a query and make it complied always. We call this type of query as complied query.
    Published Date : 08/Jan/2011
    In this article I will show, how to use the Stored Procedure with DataContext in LINQ to retrieve records from database. The Stored Procedure is most supportive to data driven application to manage performance and keep secured data retrieval.
    Published Date : 07/Jan/2011
    Var derives type from the right hand side. And its scope is in the method. And it is strongly typed. IEnumerable is interface which allows forward movement in the collection.
    Published Date : 16/Jan/2011
    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.
    Published Date : 15/Jan/2011
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
</