Export (0) Print
Expand All

How to: Retrieve Information As Read-Only (LINQ to SQL)

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.

NoteNote

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);
}


Show:
© 2014 Microsoft