if MyObjectType
is a linq-generated entity, and those objects are not already associated to a data context you can call
foreach( MyObjectType value in myList )
{
dataContext.MyObkectTypes.InsertOnSubmit(value);
}
dataContext.SubmitChanges();
However, at this time linq-to-sql isn't terribly efficient at bulk updates. If myList was 1000 items, you would have 1000 insert statements.
For very large lists you could convert the List<MyObjectType>
into xml and use sql servers ability to bulk insert using xml. You would attach the sql server stored procedure to the datacontext.
string xml = CreateInsertXml( myList );
dataContext.usp_MyObjectsBulkInsertXml(xml);
example of sql server stored procedure for bulk insert via xml
-- XML is expected in the following format:
--
-- <List>
-- <Item>
-- <PlotID>1234</PlotID>
-- <XValue>2.4</SmsNumber>
-- <YValue>3.2</ContactID>
-- <ResultDate>12 Mar 2008</ResultDate>
-- </Item>
-- <Item>
-- <PlotID>3241</PlotID>
-- <XValue>1.4</SmsNumber>
-- <YValue>5.2</ContactID>
-- <ResultDate>3 Mar 2008</ResultDate>
-- </Item>
-- </List>
CREATE PROCEDURE [dbo].usp_MyObjectsBulkInsertXml
(
@MyXML XML
)
AS
DECLARE @DocHandle INT
EXEC sp_xml_preparedocument @DocHandle OUTPUT, @MyXML
INSERT INTO MyTable (
PlotID,
XValue,
YValue,
ResultDate
)
SELECT
X.PlotID,
X.XValue,
X.YValue,
X.ResultDate
FROM OPENXML(@DocHandle, N'/List/Item', 2)
WITH (
PlotID INT,
XValue FLOAT,
YValue FLOAT,
ResultDate DATETIME
) X
EXEC sp_xml_removedocument @DocHandle
GO