.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> LINQ >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Read-Only Data in LINQ

Posted By:Dhananjay Kumar       Posted Date: January 09, 2011    Points: 75    Category: LINQ    URL: http://www.dotnetspark.com  

Read-Only Data in LINQ. By default CRUD operation can be performed on the retrieved data from LINQ query. We can modify the data fetched through LINQ query.
 

Read-Only Data in LINQ

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.



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



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.



Then we will get below exception



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

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend