Solved

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

Posted on 2011-09-06
3
693 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…

737 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