Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to Sql: Can the DataContext instance return collections that include pending changes?

Tags:

linq-to-sql

Consider the following code block:

using (PlayersDataContext context = new PlayersDataContext())
{
    Console.WriteLine(context.Players.Count()); // will output 'x'
    context.Players.InsertOnSubmit(new Player {FirstName = "Vince", LastName = "Young"});
    Console.WriteLine(context.Players.Count()); // will also output 'x'; but I'd like to output 'x' + 1
}

Given that I haven't called

context.SubmitChanges();

the application will output the same player count both before and after the InsertOnSubmit statement.

My two questions:

Can the DataContext instance return collections that include pending changes?

Or must I reconcile the DataContext instance with context.GetChangeSet()?

like image 847
Jim G. Avatar asked Aug 11 '09 15:08

Jim G.


1 Answers

Sure, use:

context.GetChangeSet()

and for more granularity, there are members for Inserts, Updates, and Deletes.

EDIT: I understand your new question now. Yes, if you wanted to include changes in the collection, you would have to somehow combine the collections returned by GetChangeSet() and your existing collections.

like image 107
JoshJordan Avatar answered Nov 15 '22 11:11

JoshJordan