HOW TO:將資訊擷取成唯讀資訊 (LINQ to SQL)
更新: November 2007
不想要變更資料時,可以搜尋唯讀結果以增加查詢效能。
將 ObjectTrackingEnabled 設定為 false,即可實作唯讀處理。
注意事項: |
---|
ObjectTrackingEnabled 設定為 false 時,DeferredLoadingEnabled 會隱含地設定為 false。 |
範例
下列程式碼會擷取員工雇用日期的唯讀集合。
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);
}