views:

1399

answers:

3

Hi,

I am trying to understand difference between those two and really need a explanatory simple example for them.

Thanks in advance..

+4  A: 

InsertOnSubmit adds a single record. InsertAllOnSubmit does the same, but for a set (IEnumerable<T>) of records. That's about it.

Marc Gravell
I think to clarify, what that means is that if you have a collection of items to submit, you would pass it to "InsertAllOnSubmit", instead of iterating over the collection and calling "InsertOnSubmit" on each item.
Andrew Theken
Indeed, that is what I mean; thanks for clarifying
Marc Gravell
Thanks :) I was a little bit confused but it's nice example to understand.
Braveyard
+5  A: 

Theres a good Q&A about this on the MSDN forums. Most interesting bit:

InsertAllOnSubmit() simply loops over all the elements in the IEnumerable collection and calls InsertOnSubmit() for each element.

Jon Skeet
+1  A: 

I found this example of InsertAllOnSubmit() at the very bottom of this page. Just remember to add a using statement for System.Collections.Generic

// Create list with new employees
List<Employee> employeesToAdd = new List<Employee>();

employeesToAdd.Add(new Employee() { EmployeeID = 1000, FirstName = "Jan", LastName = "Jansen", Country = "BE" });
employeesToAdd.Add(new Employee() { EmployeeID = 1001, FirstName = "Piet", LastName = "Pieters", Country = "BE" });
employeesToAdd.Add(new Employee() { EmployeeID = 1002, FirstName = "John", LastName = "Johnson", Country = "BE" });

// Add all employees to the Employees entityset 
dc.Employees.InsertAllOnSubmit(employeesToAdd);

// Apply changes to database
dc.SubmitChanges();
Jim Ierley