views:

392

answers:

4

What is the fastest way to get a list object with a count of 100,000 records into SQL using LINQ to SQL?

+1  A: 

You could:

MyContext.Items.InsertAllOnSubmit( 
    from i in list
    select new Item
    {
       //map properties
    });

Ps. Haven't tried inserting that much data though

eglasius
That will be very slow. LINQ-to-SQL will generate single insert statements for each record.
Kirk Broadhurst
A: 

LINQ2SQL does not support batch inserts. Look at running your own SQL, or use another framework that does support it (I recall the Entity Framework does, but I cant be sure).

leppie
Somebody have a problem with my answer? Speak up, I cant hear you.
leppie
A: 

You might want to specify exactly where is the source of your 100,000 records coming from.

If you are retrieving the records from a non-database source, the fastest way I know to insert the records is by using the InsertAllOnSubmit method. Of course, this means 100,000 insert statements in a transaction, but without further details there isn't much choice in the matter.

If you are retrieving the records from a database, the performance will be better if you call a stored procedure instead. For example, in your stored procedure:

INSERT INTO TableA
SELECT * FROM TableB

Then, you call the stored procedure with LINQ.

alextansc
Even for non-database, you can use bulk transfer via SqlBulkCopy; there are lots of ways of creating an IDataReader, not just from a db.
Marc Gravell
+3  A: 

For mass data, the fastest way is to use something like SqlBulkCopy to push the data (via a crafted IDataReader) into a staging table (same layout, but no indexes/etc) - and then use SqlCommand to execute a stored procedure to push the data from the staging table into the real table. But note that LINQ-to-SQL isn't involved here (unless you use it to invoke the stored procedure).

You could try just using regular LINQ-to-SQL InsertOnSubmit - assess how big a problem the volume of data is before you try to optimise it; it might be "fast enough" even though sub-optimal.

You can create an IDataReader fairly easily; I often use the csv on from here. To create one from a list, you could borrow SimpleDataReader from here and override DoRead.

Marc Gravell
Trying to work on this issue myself, as InsertOnSubmit for my 160,000 records has taken all day so far.
Dave Arkell
Let me know if you get stuck...
Marc Gravell