1

I have suddenly faced a problem with moving some records in a SQL Server database from one table to another, using LINQ to SQL. Is it possible to write in LINQ to SQL a query just as simple as this:

INSERT INTO Table2 SELECT * FROM Table1 WHERE Selected = 1 GO DELETE FROM Table1 WHERE Selected = 1 GO 

without using loops and collections?

2
  • To be honest, this should be a stored procedure. Using LINQ for this would be overkill. Commented Feb 24, 2010 at 9:33
  • I had same problem, and solved my problem<br> See this answer. This worked for me Commented Feb 9, 2018 at 19:45

3 Answers 3

2

You absolutely don't need LINQ here. Consider performance & maintainability implications of issuing two SQL statements (or invoking a sproc) versus having to load potentially thousands of objects and then saving them back again.

Resort to SqlCommands. That will be your best choice.

Sign up to request clarification or add additional context in comments.

1 Comment

Maybe wrap in a transaction? Maybe.
1

I'm not aware of a way to move rows in Linq2Sql without a loop. :)

This kind of thing is much easier in SQL. You can use the output into clause to move rows in a single statement:

delete from Table1 output deleted.id, deleted.name into Table2 where Selected = 1 

1 Comment

InsertAllOnSubmit, DeleteAllOnSubmit - look no (explicit) loop (-: I quite agree that Linq to SQL is not the appropriate solution though.
0

THis is just a simple insert and delete method which should written together.

One insert necessary fields to second table and when its finished, the second statement will remove the fields from first Table

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.