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.

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.

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

See Also

Concepts

Deferred versus Immediate Loading (LINQ to SQL)

Other Resources

Query Concepts in LINQ to SQL

Querying the Database (LINQ to SQL)