0
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; }
}
Habib
  • 219,104
  • 29
  • 407
  • 436
vini
  • 4,657
  • 24
  • 82
  • 170
  • 3
    Try overloading `Equals` and `GetHashCode` in your `SessionResponseList` class. See this article for info: http://www.infoq.com/articles/Equality-Overloading-DotNET – Alxandr Jul 17 '13 at 13:52
  • Just try with Enumerable.SequenceEqual(list1,list2) to check your two generic list are actually same. see http://stackoverflow.com/questions/2270754/how-to-compare-two-generic-lists-in-c-sharp-3-0 – Rezoan Jul 17 '13 at 14:08

1 Answers1

1

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)
Habib
  • 219,104
  • 29
  • 407
  • 436