[英]Not able to remove a Generic List object in c#
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; }
}
與其創建一個新實例, FirrstOrDefault
嘗試使用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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.