How to retrieve a generic list from session?

前端 未结 5 2158
[愿得一人]
[愿得一人] 2021-02-09 19:12

I have a list that I put in session:

Session.Add(\"SessionList\", mylist);

How to retrieve it back from the session?

相关标签:
5条回答
  • 2021-02-09 19:25

    Try

    var myList = (List<WhateverTypeItIs>)Session["SessionList"];
    
    0 讨论(0)
  • 2021-02-09 19:35

    liek this...

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

    or you can cast like this

     var List1 = (List<typespecified>)Session["SessionList"];  
    
    0 讨论(0)
  • 2021-02-09 19:35

    Like below

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

    OR also you can cast like below

    var list = (List<typespecified>)Session["SessionList"];
    
    0 讨论(0)
  • 2021-02-09 19:36

    Try this:

    Var sessionlist = (List<Type of list>) Session["CustomerSessionList"];
    
    0 讨论(0)
  • 2021-02-09 19:52
    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.

    0 讨论(0)
提交回复
热议问题