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.

Note

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

Example

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);
}
Dim db As New Northwnd("c:\northwnd.mdf")

db.ObjectTrackingEnabled = False
Dim hireQuery = _
    From emp In db.Employees _
    Select emp _
    Order By emp.HireDate

For Each empObj As Employee In hireQuery
    Console.WriteLine("EmpID = {0}, Date Hired = {1}", _
            empObj.EmployeeID, empObj.HireDate)
Next

See Also

Query Concepts
Querying the Database
Deferred versus Immediate Loading