linq - Combine two records into one and update column -


i have following records in cartitems table:

enter image description here

when migrate cart, i'd combine 2 records 1 cart id of test1 , update quantity 2.

so cartitemid 84, cartid test1, quantity 2, datecreated 5/21/2013, productid 16

how in linq/ef?

edit

 public void migratecart(string username)     {         shoppingcartid = getcartid();          var shoppingcart = _context.shoppingcartitems.where(c => c.cartid == shoppingcartid);          foreach (var item in shoppingcart)         {             item.cartid = username;         }          _context.savechanges();      } 

edit

i think have it.

public void migratecart(string username)     {         shoppingcartid = getcartid();          if (shoppingcartid != username)         {             var shoppingcart = _context.shoppingcartitems.where(c => c.cartid == shoppingcartid).tolist();             var usershoppingcartitems = _context.shoppingcartitems.where(c => c.cartid == username).tolist();              foreach (var item in shoppingcart)             {                 foreach (var usershoppingcartitem in usershoppingcartitems)                 {                     if (item.productid == usershoppingcartitem.productid)                     {                         item.quantity += usershoppingcartitem.quantity;                         item.cartid = username;                         _context.shoppingcartitems.remove(usershoppingcartitem);                     }                 }                 item.cartid = username;             }              _context.savechanges();          }         else         {             var shoppingcart = _context.shoppingcartitems.where(c => c.cartid == shoppingcartid);              foreach (var item in shoppingcart)             {                 item.cartid = username;             }              _context.savechanges();         }      } 

as don't ask how 2 items, give rustic way.

var item1 = context.cartitems.firstordefault(m => m.id == 84); var item2 = context.cartitems.firstordefault(m => m.id == 85);  item1.quantity += item2.quantity;//add quantity of item2 item1 context.cartitems.remove(item2);//remove item2 context.savechanges(); 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -