Solved

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

Posted on 2011-09-06
3
675 Views
Last Modified: 2013-11-11

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"];
0
Comment
Question by:mmalik15
  • 2
3 Comments
 
LVL 5

Expert Comment

by:zvytas
Comment Utility
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.
0
 
LVL 5

Accepted Solution

by:
zvytas earned 250 total points
Comment Utility
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.
0
 
LVL 19

Assisted Solution

by:Bardobrave
Bardobrave earned 250 total points
Comment Utility
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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now