When we perform queries and retrieve objects like the
product instances above, LINQ to SQL will by default keep track of any changes
or updates we later make to these objects. We can make any number of
queries and changes we want using a LINQ to SQL DataContext, and these changes
will all be tracked together.
Note: LINQ to SQL change tracking happens on the consuming
caller side - and not in the database. This means that you are not
consuming any database resources when using it, nor do you need to
change/install anything in the database to enable it.
After making the changes we want to the objects we've
retrieved from LINQ to SQL, we can then optionally call the
"SubmitChanges()" method on our DataContext to apply the changes back
to the database. This will cause LINQ to SQL to dynamically calculate and
execute the appropriate SQL code to update the database.
For example, I could write the below code to update the
price and # of units in stock of the "Chai" product in
the database:
Figure 4

When I call northwind.SubmitChanges() above,
LINQ to SQL will dynamically construct and execute a SQL "UPDATE"
statement that will update the two product property values we modified above.
I could then write the below code to loop over
unpopular, expensive products and set the "ReorderLevel" property of
them to zero:
Figure 5

When I call northwind.SubmitChanges() above, LINQ to SQL
will calculate and execute an appropriate set of UPDATE statements to modify
the products who had their ReorderLevel property changed.
Note that if a Product's property values weren't changed by
the property assignments above, then the object would not be considered changed
and LINQ to SQL would therefore not execute an update for that product back to
the database. For example - if the "Chai" product's unitprice
was already $2 and the number of units in stock was 4, then calling
SubmitChanges() would not cause any database update statements to
execute. Likewise, only those products in the second example whose
ReorderLevel was not already 0 would be updated when the SubmitChanges() method
was called.