How to retrieve a generic list from session?

倖福魔咒の 提交于 2019-12-04 13:27:31

问题


I have a list that I put in session:

Session.Add("SessionList", mylist);

How to retrieve it back from the session?


回答1:


var list = Session["SessionList"] as List<whatevertypeYouUsed>;

if (list != null){
   // blah...
}

I prefer to use the as keyword since there is no 100% guarantee that the Session will contain the list (due to application pool refresh, website being restarted, etc). Gives you that extra bit of defence to avoid a NullReferenceException.




回答2:


Try

var myList = (List<WhateverTypeItIs>)Session["SessionList"];



回答3:


liek this...

 var list = Session["SessionList"] as List<whateveritis>;

or you can cast like this

 var List1 = (List<typespecified>)Session["SessionList"];  



回答4:


Like below

 var list  = Session["SessionList"] as List<typespecified>;

OR also you can cast like below

var list = (List<typespecified>)Session["SessionList"];



回答5:


Try this:

Var sessionlist = (List<Type of list>) Session["CustomerSessionList"];


来源:https://stackoverflow.com/questions/8194238/how-to-retrieve-a-generic-list-from-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!