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

请帮忙。

 public class SessionResponseList{

    public int QId { get; set; }
    public string QAnswer { get; set; }
}
4

1 回答 1

1

而不是创建一个新实例,您应该尝试使用从列表中获取实例FirrstOrDefault,如果找到,则从列表中删除该实例,目前您正在创建一个新对象,并且您正在尝试从列表中删除它。

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)
于 2013-07-17T13:54:29.877 回答