Reputation: 4742
SessionResponseList objClientSessionResponseList = new SessionResponseList();
objClientSessionResponseList.QId = Convert.ToInt32(Session["QuestionNumber"]);
objClientSessionResponseList.QAnswer = Session["CurrentAnswer"].ToString();
objSessionResponseList = (List<SessionResponseList>)Session["Answers"];
if (objSessionResponseList.Where(x=>x.QId == objClientSessionResponseList.QId && x.QAnswer==objClientSessionResponseList.QAnswer).Count()>0)
{
objSessionResponseList.Remove(objClientSessionResponseList);
Session["Answers"] = objSessionResponseList;
}
// objSessionResponseList.Remove(objClientSessionResponseList);
//This isn't working tried everything the values are exact duplicate
Please help.
public class SessionResponseList{
public int QId { get; set; }
public string QAnswer { get; set; }
}
Upvotes: 0
Views: 163
Reputation: 223247
Instead of creating a new instance you should try getting the instance from the List using FirrstOrDefault
and if that is found then remove that instance from the list, currently you are creating a new object and you are trying to remove that from the list.
var itemToBeRemoved = objSessionResponseList
.FirstOrDefault(x=>
x.QId == Convert.ToInt32(Session["QuestionNumber"]) &&
x.QAnswer == Session["CurrentAnswer"].ToString();
if(itemToBeRemoved != null) //means item is found in the list
objSessionResponseList.Remove(itemToBeRemoved)
Upvotes: 1