[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1736
  • Last Modified:

Linq to Sql, InsertAllOnSubmit

using linq to sql, I need to insert two records on each iteration through a loop (perhaps 2000 times)

I know I can use this technique
    table_sql tbl1 = new table_sql();
    // set the column values for table_sql
    dc.table_sqls.InsertOnSubmit(tbl1);

    table_sql tbl2 = new table_sql();
    // set the column values for table_sql
    dc.table_sqls.InsertOnSubmit(tbl2);

    Then do dc.SubmitChanges from the calling loop (because other things happen in the loop, too)

I assume, however, that it would be better to use an InsertAllOnSubmit for this in that it would cut the number of connection open/close operations in half.

How do I use InsertAllOnSubmit for this?
0
g_johnson
Asked:
g_johnson
1 Solution
 
Fernando SotoCommented:
Hi g_johnson;

Your assumption is not correct. the InsertAllOnSubmit method will adds all entities of a collection to the DataContext in a pending insert state. So if you were to go through the loop and each time through you created an instance of and entity and set its values and add this entity to a List<EntityType> then when you completed going through the look you can add the list of entities all at once to the data context. Then when you do the dc.SubmitChanges the data context will go through the inserted entities and update the data store / database one insert at a time. The Linq query technology was NOT created to do mass updates.

Fernando
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now