user2380844
user2380844

Reputation: 307

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.

Upvotes: 2

Views: 2746

Answers (3)

Dave
Dave

Reputation: 8451

You can use Remove

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

Upvotes: 2

Ansh
Ansh

Reputation: 51

You can do it like this

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

Upvotes: 0

Damith
Damith

Reputation: 63065

you have to edit the condition accordingly

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

Upvotes: 2

Related Questions