Copy values from one table to another table using LINQ to SQL?


I have two database tables that have different names but identical structure (columns).

What is the easiest way to copy the values from one table to the other table using LINQ to SQL? 


-- shawn

9/20/2008 6:22:11 AM 91979 articles. 4 followers. Follow

1 Replies

Similar Articles

[PageSpeed] 5


As far as I research, we could use "foreach"  to insert the data from one table to another table.

The code is shown below.

        //there exist two table list and listSecond
        DataClassesDataContext dataClass = new DataClassesDataContext(); //create the instance of the DataContext

        var str = from a in dataClass.lists select a;
        foreach (var val in str) // iterator the data from the list and insert them into the listSecond
            listSecond ls = new listSecond();
            ls.ID = val.ID;
            ls.url = val.url;



Please feel free to let me know if I’ve misunderstood anything.

Zong-Qing Li
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as
Answer” if a marked post does not actually answer your question.
9/24/2008 2:56:59 AM

Similar Artilces:

Updating another table using linq to sql table.
Hi,After successfully saved the values from the Rent Table, I want to update the Status attribute from "Available" to "On Booking" for that particular bike.The Status attribute is taken from the Bike Table.And I'm using linq to sql to save the values from the Rent Table.Here is how I save the Rent table without updating the "Status" attribute:  using (DataClassesDataContext context = new DataClassesDataContext()) { Rent rent = new Rent { NRIC = nrictxtbox.Text, Bikeid = Convert.ToInt32...

How do i copy / insert a Primary Key value of one table into another table?
  Hi i have set up  two very simple tables, I want a user to be able to create a basic account ( data stored in User_Profile  table with Id set as the Primery Key as Identity) I want the user to be able to be able to return to their account at a later date and then post multiple reviews of different bands they have seen at a later date. I kept the tables in my example very simple so I could get my head around the concept, but generally, I want to connect the Id (PK) value in User_Profile table to the User_Id filed in the User_Review table, so every review th...

CheckBoxList gets text from one table, send values to another table
I am working on making a CheckBoxList in a DetailsView that allows the user to check the boxes of the services they would like to receive. In my SQLDataSource, I have two tables that are involved in this. Columns "ServiceID" and "Name"are stored in one table, "Services," and the columns "UserID" and "ServiceID" are stored in another table, "UserServices." I want the CheckBoxList to grab the value and the name of all the services available and have a box for each with the name beside each box. Once the user clicks on each box they want and then click "Submit" I want all rows in table "Use...

is there an sql statement to insert data to one table and getting the data from another table
Ok I have a table that have general categories with just name and CatNo. Now what I want to know is when I create a client to move all the data in general categories to another table called categories that have the clientid to associate it with the client. I would appreciate for any help what is your question here??Shravan Addaypally MCP...

ddl using LINQ: Need to set WHERE clause of LINQ data source to another table's PK. (use LINQ to join FK to seperate table's PK as part of WHERE)
This is a bit odd, but I have a drop down list that I want to use to display a list of values. These values are a list of products.  I want to filter this list of products by the product category.  HOWEVER, productCategory is NOT part of the products table.  There is a seperate many to many table that handles that. (Some products could be in multiple categories like how a video game at a store could be considered to be part of "Video Games", "Educational Games", "Games for the Xbox360" or whatever might make ense).  So what I have is: A li...

Copying Binary data from one SQL table to another using XML
 Hi,     I have encrypted data stored as in binary format in a table .The data need to be copied into another table based on some conditions.I'm pasing the source table data to the SP in XML format. After inserted into the destination table , If I either try to compare both columns directly or after the decryption the values does not match.  I'm using ASP.Net2.0 with SQL server2005.The source table data is converted into XML document with   FOR XML PATH('employee'), ROOT('root'), BINARY BASE64 , ELEMENTS  and passed into SP...

insert multiple values from one table into one column from another table?
Hi, I'm attempting to create an insert statement that gets the values from multiple columns and insert them into one column from another table, is there a simple solution to do this?Here is something similar to what i'm wanting done, just it's not the correct way to do it, i want value1, value2 and value3 from one table to be combined and put into the value1 column of another table, what am i doing wrong? INSERT INTO tablename2 (value1 + value2 + value3)SELECT value1FROM tablename1 thanks in advance! INSERT INTO tablename2 (SELECT val1 + val2 + val3 FROM tabl...

copy column from one table to another table
Hi, I have 2 tables. Table1 has column1 and column2. I wish to copy column1 into Table2 by using sql statement. How do i do that? Thank you. In general you'd do it like this: Insert statement: INSERT INTO Table2 ( Column1 ) SELECT Column1 FROM Table1 Update statement: UPDATE Table2 SET Column1 = T1.Column1 FROM Table2 T2 INNER JOIN Table1 T1 ON T2.someID = T1.someID TerriTerri MortonEngagement Manager, NeudesicHow to ask a question...

try to copy data from one table into another table
PB8.0 , i built two tables, one is customer table,one is hosts table, they have some same columns,like customer name, company name ,etc. now, i built a datawindow of the customers, after i retrieve datas from the customer table to the datawindow , i hope i can copy datas of same columns from customer table to host table, so that i dont have to input same data again for the host table. what ways i can use and how to do? Use a Pipeline, at dawn datawindow. BMartins <jennifer> escreveu na mensagem > PB8.0 , i built two ta...

Copying html table rows from one table to another.
Hi Im am trying to copy table rows from one html table to another. Below is a one of my many attempts and all this does is moving the rows from the one table to the other. I would appreciate any help. int iCount; HtmlTableRow[] TableRows = new HtmlTableRow[Table1.Rows.Count]; Table1.Rows.CopyTo(trs,0); for (iCount = 0; iCount < TableRows.Length; iCount++) { Table2.Rows.Add(TableRows[iCount]); } Thanks ...

Copy Records From one table to another table with same structure
Hi allI have two tables in SqlServer with Exactly Same Structure,I want to Copy all Records fromone of them to another one.I came across to "" statement But i have two problem 1) I don't know any thing about Columns name!!! i just know they have same structure and as far as i know , "" need the Column list to operate correctly, am i right? 2) these two table have One Prinary Key column with IDENTITY feature. Any Help Greatly appriciated.Regards.Nothing Is Quite Impossible !!! Personally, i found DTS is the best way you just need to set&nbs...

can I compare one record from one table to another record in another table?
can I compare one record from one table to another record in another table? I need to test if this condition is met. My scenario is: i need to check one table for a value and compare it to a list of records. for example, table 1           columnrecord 1- applesrecord 2- orangesrecord 3- grapes table 2            col1   col2   col3    col4record 1- Null , Null, Null, grapes I need to compare table 2 records with a list of records from table 1. the fru...

DropDownList bound to one table, selected value from another table
Hi I am having a very frustrating problem with a DropDownList.  The DropDownList is bound to one SQL Server 2005 table.  I do this progromatically with the following code:Public Sub EvaltrID() TryDim aType As String = txtAircraftType.SelectedValue Dim auth As String = Convert.ToInt32(MinLevel.Text) Dim active As String = "True" Dim iPilot As String = "True"Dim CrewPos As String = txtCrewPos.SelectedValue Dim CrewPos1 As String = "SO" Dim instructSO As String = "True"Dim sesType As String = txtSessionType.Text Dim sesType1 As String ...

DropDownList bound to one table, selected value from another table
I posted this in another forum topic and I'm not getting any response.  I'm hoping maybe I will ge more help here since it pertains to a server control.  Hi I am having a very frustrating problem with a DropDownList.  The DropDownList is bound to one SQL Server 2005 table.  I do this progromatically with the following code:Public Sub EvaltrID() TryDim aType As String = txtAircraftType.SelectedValue Dim auth As String = Convert.ToInt32(MinLevel.Text) Dim active As String = "True"Dim iPilot As String = "True" Dim CrewPos As Strin...

Web resources about - Copy values from one table to another table using LINQ to SQL? -

Magid: Precautions for holiday shopping
With the holiday season upon us, a lot of people will be shopping online this year. For the most part, it's pretty safe but there are some basic ...

Germany's Merkel vows more support for France after attacks
PARIS (Reuters) - Chancellor Angela Merkel said on Wednesday she would think about how Germany could further support France in fighting terrorism ...

Black Friday Streaming Devices: Best Deals for Chromecast, Roku, Apple TV and More
Black Friday is nearly upon us, and this year, consumers around the country are once again looking for devices that help them to stream their ...

‘Star Wars’ Charity Contest Gives Fans a Chance to Attend ‘Force Awakens’ Premiere
For a $10 charitable donation, “Star Wars” fans have the chance to attend the premiere of “The Force Awakens” in Los Angeles or London. In a ...

One Of The Downed Su-24 Pilots Is Alive And Talking As Russia Beefs Up Defenses In Syria
It has been more than 24 hours since Turkey shot down a Russian Su-24 Fencer that briefly passed into its territory . Now we are seeing the clear ...

Facts? Donald Trump Don’t Need No Stinkin’ Facts
In an exchange with Bill O’Reilly Monday night, Donald Trump summed up so much of his Presidential campaign in one short statement: File this ...

What We Learned/What You May Have Missed In The Captain America Civil War Trailer
The Captain America: Civil War trailer was unleashed last night, and now that we’ve watched the teaser a few times we’re ready to assess the ...

Bernie Sanders wants to raise wages of H-1B workers
U.S. Sen. Bernie Sanders (I-VT) wants to reform the H-1B program, in part, by "substantially" raising prevailing wages. Higher wages is one ...

Macy's Thanksgiving Day Parade 2015
There’s just one day left until the 2015 Macy’s Thanksgiving Day Parade and we’re so excited about all the performers set to make appearances ...

Google's Accelerated Mobile Pages will speed up the web in 2016
It's now a few weeks since Google announced Accelerated Mobile Pages . When the project was unveiled, there were just 30 or so partners on board ...

Resources last updated: 11/25/2015 8:19:59 PM