Solved

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

Posted on 2011-09-06
3
678 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 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

13 Experts available now in Live!

Get 1:1 Help Now