Linq to Sql, InsertAllOnSubmit

Posted on 2011-04-25
Last Modified: 2013-11-11
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

    table_sql tbl2 = new table_sql();
    // set the column values for table_sql

    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?
Question by:g_johnson
    1 Comment
    LVL 62

    Accepted Solution

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now