• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2135
  • Last Modified:

how to store and retrieve Linq List into session variable in c#

This is how i am storing my linq list into session. How would I retrive it back from session

 DataClassesDataContext myContext = new DataClassesDataContext();

        var QuestionList = myContext.sp_GetQuestionsByChapterID(Session["QuizID"].ToString()).ToList();

        Session["QuestionsList"] = QuestionList;

I have tried something like this to retrive it back from session

var QuestionList=  Session["QuestionsList"] ;
int QuestionID = (int)QuestionList[CurrentPos].QuestionID;

this is giving me an error
"Cannot apply indexing with [] to an expression of type 'object'"


0
mmalik15
Asked:
mmalik15
  • 2
1 Solution
 
Kusala WijayasenaSoftware EngineerCommented:
What would be the type of "QuestionList" ? Is it "List<Question>" ?

If "QuestionList" is "List<Question>", you can retrieve as follows

List<Question> questions = (List<Question>)Session["QuestionsList"];
int questionID = questions[CurrentPos].QuestionID;

Open in new window


-Kusala

 
0
 
mmalik15Author Commented:
thanks for comment
I store QuestionList  in var like this, var QuestionList  but using it as an arraylist. Can we convert the session into arraylist?
0
 
Kusala WijayasenaSoftware EngineerCommented:
You can cast it like this:

ArrayList questionList =  (ArrayList)Session["QuestionsList"];

Open in new window


-Kusala
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now