I have a table in the database that I’m retrieving using LINQ to SQL, and as a part of my processing I want to add to this list, then update the database with the new items + any changes I’ve made.
What I thought I could do was this:
var list = (from item in db.Table select item).ToList(); [do processing where I modify items & add to the list] list = list.Distinct(); db.SubmitChanges();
What happens is that the modifications happed (ie. SQL updates) but any new items I add to the list don’t get added.
Obviously I’m doing this wrong, what is the correct way to modify & add to a list of DB entities, then commit all the updates & inserts?
The List is meaningless. It’s just happens to hold objects that the DataContext knows about. We need to make sure that the DataContext knows about the new ones. The important thing is that they don’t have to be complete when we alert the DataContext to them: