Vetri
Vetri

Reputation: 347

Retrieve list from session variable in MVC4?

I am new to Asp.Net MVC4. I am storing a list to Session. From that session I am trying to retrieve list from session variable. After getting it i want to get a row values based on index value. How to get it please help me to solve this problem. Thanks in advance

This is my LINQ result to Session:

var notifications = (from t in db.Tbl_Tasks
                                     join tu in db.Tbl_User_Task on t.TaskId equals tu.Task_Id
                                     where u.UserID==Id && t.TaskStart == startdate && t.TaskEnd == enddate 
                                     orderby t.TaskStart
                                     select new
                                     {
                                         t.TaskId,
                                         t.Task,
                                         tu.AdminComment,
                                     }).ToList(); 
    var NotificationList = notifications.GroupBy(x => x.Task_User_Id).Select(group => group.LastOrDefault()).ToList();
    Session["MyTaskListSession"] = NotificationList;  

I am getting that session like this:

var myTaskList= Session["MyTaskListSession"];

Upvotes: 0

Views: 1662

Answers (1)

dotnetstep
dotnetstep

Reputation: 17485

I would suggest following thing.

Create One Class.

public class Test
{ 
   public int TaskId {get;set;}
   public string Task {get;set;}
   public string AdminComment {get;set;}
}

var notifications = (from t in db.Tbl_Tasks
                                     join tu in db.Tbl_User_Task on t.TaskId equals tu.Task_Id
                                     where u.UserID==Id && t.TaskStart == startdate && t.TaskEnd == enddate 
                                     orderby t.TaskStart
                                     select new Test
                                     {
                                       TaskId=  t.TaskId,
                                       Task =  t.Task,
                                       AdminComment =  tu.AdminComment,
                                     }).ToList(); 
    var NotificationList = notifications.GroupBy(x => x.Task_User_Id).Select(group => group.LastOrDefault()).ToList();
    Session["MyTaskListSession"] = NotificationList; 

you can get session value like this.

var myTaskList= (List<Test>)Session["MyTaskListSession"];

Note : When you not specify any type it will create anonymous type. It is generated automatically and we don't know its type. So either you have to use reflection or use strongly type class like "Test".

so you can later work on that.

if you don't want to use Strongly type then and If I assume that you are using .NET Framework 4.0 then you can do like this.

var notifications = (from t in db.Tbl_Tasks
                                     join tu in db.Tbl_User_Task on t.TaskId equals tu.Task_Id
                                     where u.UserID==Id && t.TaskStart == startdate && t.TaskEnd == enddate 
                                     orderby t.TaskStart
                                     select new
                                     {
                                         t.TaskId,
                                         t.Task,
                                         tu.AdminComment,
                                     }).ToList(); 
    var NotificationList = notifications.GroupBy(x => x.Task_User_Id).Select(group => group.LastOrDefault()).ToList();
    Session["MyTaskListSession"] = NotificationList;  

when you retrieve then do

dynamic myTaskList  =   Session["MyTaskListSession"];

Upvotes: 1

Related Questions