Solved

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

Posted on 2011-09-06
3
682 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
ID: 36488109
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
ID: 36488133
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
ID: 36488334
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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