How to: Retrieve Information As Read-Only


When you do not intend to change the data, you can increase the performance of queries by seeking read-only results.

You implement read-only processing by setting ObjectTrackingEnabled to false.

System_CAPS_ICON_note.jpg Note

When ObjectTrackingEnabled is set to false, DeferredLoadingEnabled is implicitly set to false.

The following code retrieves a read-only collection of employee hire dates.

            Northwnd db = new Northwnd(@"c:\northwnd.mdf");

            db.ObjectTrackingEnabled = false;
            IOrderedQueryable<Employee> hireQuery =
                from emp in db.Employees
                orderby emp.HireDate
                select emp;

            foreach (Employee empObj in hireQuery)
                Console.WriteLine("EmpID = {0}, Date Hired = {1}",
                    empObj.EmployeeID, empObj.HireDate);

Query Concepts
Querying the Database
Deferred versus Immediate Loading