Priyank Patel
Priyank Patel

Reputation: 7006

How to insert a row from one table to another table using Linq to SQL

I am having to two tables Data_Cust_Log and Data_Cust.The structure of both the table is same.When a customer is authorized the row of data for that customer from Data_Cust_Log needs to be copied to Data_Cust.Can anybody let me know if this can be done using Linq to SQL. Any suggestions are welcome. Thanks.

Upvotes: 0

Views: 4559

Answers (1)

formatc
formatc

Reputation: 4323

If the objects are of the same type:

 using (DataClasses1DataContext context = new DataClasses1DataContext())
 {
    var data = context.Data_Cust_Log.Where(x => x.CustomerID == 12) Select(x => x).FirstOrDefault();
    context.Data_Cust.InsertOnSubmit(data);
    context.SubmitChanges();
 }

If they are not of the same type:

using (DataClasses1DataContext context = new DataClasses1DataContext())
{
    var data = context.Data_Cust_Log.Where(x => x.CustomerID == 12) Select(x => x).FirstOrDefault();

    Data_Cust_Object = new Data_Cust_Object {CustomerID = data.CustomerID, Price = data.Price}; //and so on 

    context.Data_Cust.InsertOnSubmit(Data_Cust_Object);
    context.SubmitChanges();
}

Upvotes: 3

Related Questions