how to retreive LINQ object from a session variable in c#

mmalik15 used Ask the Experts™

My stored procedure is returing me data from multiple tables and I am using LINQ as following to store the data in an object of "Var" type using .ToList() extension method. I need to store this list in a session variable and need to retrieve the object in the same .ToList() Type.

DataClassesDataContext myContext = new DataClassesDataContext();

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

Session["QuestionsList"] = QuestionList;

how can i possibly retrieve it from session?

As I have mentioned sp_GetQuestionsByChapterID is returnig me data from multiple tables and I cant use Question Object like following

List<Question> questions = (List<Question>)Session["QuestionsList"];
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

I think the only way out of this situation is defining a type that you want to use for results returned from sp_GetQuestionsByChapterID. This would mean changing the following:

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

to the following:

List<SomeType> QuestionList = myContext.sp_GetQuestionsByChapterID(Session["QuizID"].ToString()).ToList();

The code you're using right now uses anonymous types, which means you cannot retrieve the object from Session as the type is unknown.
Actually, I think the following hack chould also work:

var QuestionList = myContext.sp_GetQuestionsByChapterID("-1").ToList();
QuestionList = (typeof(QuestionList))Session["QuestionsList"];

However I would not recommend it.
If I've understood you, the main reason why you cannot use a defined type List<Question> is because your stored procedure returns you data from several tables (each of one of a different "question" type I supose).

However, I also supose probably those different types have much in common, so you probably could create a super class where different questions types derive (or a question interface wich all your question types implements).

You can use a List of this super class or interface to retrieve the list from session.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial