DataContext.GetChangeSet 方法


取得 DataContext 所追蹤的已修改物件。Gets the modified objects tracked by DataContext.

 System::Data::Linq::ChangeSet ^ GetChangeSet();
public System.Data.Linq.ChangeSet GetChangeSet ();
member this.GetChangeSet : unit -> System.Data.Linq.ChangeSet
Public Function GetChangeSet () As ChangeSet


這組物件是以三個唯讀集合傳回。The set of objects is returned as three read-only collections.


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

var custQuery =
    from cust in db.Customers
    where cust.City == "London"
    select cust;
foreach (Customer custObj in custQuery)
    Console.WriteLine("CustomerID: {0}", custObj.CustomerID);
    Console.WriteLine("\tOriginal value: {0}", custObj.City);
    custObj.City = "Paris";
    Console.WriteLine("\tUpdated value: {0}", custObj.City);
ChangeSet cs = db.GetChangeSet();
Console.Write("Total changes: {0}", cs);
// Freeze the console window.

Dim db As New Northwnd("c:\northwnd.mdf")

Dim custQuery = _
    From cust In db.Customers _
    Where (cust.City = "London") _
    Select cust

For Each custObj As Customer In custQuery
    Console.WriteLine("CustomerID: {0}", custObj.CustomerID)
    Console.WriteLine(vbTab & "Original value: {0}", custObj.City)
    custObj.City = "Paris"
    Console.WriteLine(vbTab & "Updated value: {0}", custObj.City)

Dim cs As ChangeSet = db.GetChangeSet()
Console.Write("Total changes: {0}", cs)
' Freeze the console window.



請注意下列考慮:Note the following considerations:

  • GetChangeSet可能會有副作用,例如推斷通常會在時SubmitChanges執行的插入和刪除作業。GetChangeSet might have side effects, such as inference of insert and delete operations that are usually performed at the time of SubmitChanges. 例如,下列作業中使用的物件可以在下列清單中建立對應的推斷作業:For example, objects that are used in the following operations can create corresponding inferred operations in the following list:

  • 此集合可能不會根據外鍵條件約束進行排序。The set may not be ordered according to foreign key constraints.

  • 資料庫產生的值(例如,主要和外鍵值、時間戳記等等)無法使用。Database-generated values (for example, primary and foreign key values, timestamps, and so forth) are not available. 這類資訊需要執行資料庫命令,而且可能會傳播抓取的資訊(例如,主鍵的外鍵)。Such information requires database command execution and perhaps the propagation of retrieved information (for example, foreign key from primary key).

  • 在呼叫時,會計算一組已變更的物件。The set of changed objects is computed at the time of the call. 如果進行其他SubmitChanges變更,後續的呼叫可能會產生不同的集合。Subsequent calls to SubmitChanges can produce a different set if additional changes are made.

未進行任何變更時的輸出會如下所示:Output when no changes have been made appears as follows:

{Added: 0, Removed: 0, Modified: 0}