I want to move 2 columns of my table to an another table. I can select values that i want to move but i'am unable to move them. How can i make this ? Here is my implementation
var cartQuery = (from c in db.CartTbls.AsQueryable()
join cs in db.CartShoeTbls on c.CartID equals cs.CartID
where c.CustomerID == cusID
select new {c.CustomerID,c.TotalPrice,cs.ShoeID,cs.Quantity});
What i want to do is copy the all values of ShoeID and Quantity attiributes. Any help would be appriciated.
Use should add the selection to the table and save the changes:
foreach (var item in cartQuery)
db.OrderShoeTbl.Add(new OrderShoe()
{ ShoeID = item.ShoeID, Quantity = item.Quantity });
db.SaveChanges();
if you are using Linq to SQL, the insert is different and should look like this:
foreach (var item in cartQuery)
db.OrderShoeTbl.InsertOnSubmit(new OrderShoe()
{ ShoeID = item.ShoeID, Quantity = item.Quantity });
db.SubmitChanges();
Stefan's answer is OK however you may insert everything in one step:
var cartQuery = (from c in db.CartTbls.AsQueryable()
join cs in db.CartShoeTbls on c.CartID equals cs.CartID
where c.CustomerID == cusID
select new OrderShoeTbl{ShoeID = c.ShoeID, Quantity = c.Quantity});
db.OrderShoeTbl.InsertAllOnSubmit(cartQuery);
db.SubmitChanges();
EDIT
If you want to delete data from source table I would also wrap everything in single transaction to avoid inconsistency in case of errors.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.