Solved

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

Posted on 2011-09-05
3
1,704 Views
Last Modified: 2013-11-11
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
Comment
Question by:mmalik15
  • 2
3 Comments
 
LVL 11

Expert Comment

by:Kusala Wijayasena
ID: 36485945
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
 

Author Comment

by:mmalik15
ID: 36486021
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
 
LVL 11

Accepted Solution

by:
Kusala Wijayasena earned 500 total points
ID: 36491404
You can cast it like this:

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

Open in new window


-Kusala
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net/LINQ: ".Contains" condition not finding existing result in MSSQL 16 36
Linked Server Issue with SQL2012 3 26
C# Gridview 1 33
RegEx with optional part 4 25
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

822 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question