I have a single linq to sql class. I would like to detect if it is new (meaning an insert will be done) or if there are any pending changes (update will be done). I realize that I can do this by using a partial
class and hooking into the on change events for each property. However that is a lot of maintenance for a class that is constantly changing.
Is there a better way?
While the LINQ methods always return a new collection, they don't create a new set of objects: Both the input collection (customers, in my example) and the output collection (validCustomers, in my previous example) are just sets of pointers to the same objects.
LINQ to SQL was the first object-relational mapping technology released by Microsoft. It works well in basic scenarios and continues to be supported in Visual Studio, but it's no longer under active development.
You can update rows in a database by modifying member values of the objects associated with the LINQ to SQL Table<TEntity> collection and then submitting the changes to the database. LINQ to SQL translates your changes into the appropriate SQL UPDATE commands.
LINQ queries return results as objects. It enables you to uses object-oriented approach on the result set and not to worry about transforming different formats of results into objects. The following example demonstrates a simple LINQ query that gets all strings from an array which contains 'a'.
You can check through the DataContext
class.
First, check the ObjectTrackingEnabled
property on the DataContext
. If it returns false, then the object is not being tracked by the context.
Then, call the GetChangeSet
method on the DataContext
. From there, compare the references exposed by the Deletes
, Updates
, and Inserts
properties against your object.
If the reference is found in any of those lists, then your object is being tracked by that list and you can proceed from there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With