TAGS :Viewed: 19 - Published at: a few seconds ago

[ Remove a particular item from a list which is in a session ]

List<ShoppingCartView> removeitem = (List<ShoppingCartView>)Session["ShoppingView"];

I want to remove a row on clicking that particular product. For example if I click on product xxx and want to remove the product xxx from Session["ShoppingView"], the rest of the list should remain the same.

Answer 1


you have to edit the condition accordingly

var list = (List<ShoppingCartView>)Session["ShoppingView"];
Session["ShoppingView"] = list.Where(x => x.ProductName!= "pname").ToList();

Answer 2


You can use Remove

Session["ShoppingView"] = (List<ShoppingCartView>)Session["ShoppingView"].Remove((List<ShoppingCartView>)Session["ShoppingView"].Where(x => x.id== sessionItemToRemove).ToList());

Answer 3


You can do it like this

List<string> foo =  Session["ShoppingView"];  
foo.Remove("xxx");  
Session["ShoppingView"] = foo;