C# – access the list of InsertOnSubmit’d records from a linq-to-sql DataContext

clinqlinq-to-sql

I have some code where I am using a Linq-To-SQL DataContext to add and modify records in a table.

I've run into a situation where I add a few records via InsertOnSubmit, and then I want to modify a record.. but this record may already be in the table, or it may be one of the ones I am inserting. So this:

db.MyTable.Single(t => t.Id == WhichId).Name="foobar";

Might not work, since I might not have actually inserted the record with an Id of WhichId yet.

I don't really want to SubmitChanges() until I've done everything I'm going to do.

The DataContext must keep a list of records to be inserted on SubmitChanges() – can I access that list? I'm thinking of something like this:

(db.MyTable.SingleOrDefault(t => t.Id == WhichId) ??
 db.[list of records to be inserted].Single(t => t.Id == WhichId)
).Name="foobar";

So the question is, is there anything I can put where those square brackets are?

Best Answer

db.GetChangeSet().Inserts