Store List of objects in Session variable

asp.net-mvc c# entity-framework linq-to-sql session-variables

Question

I want to store the result of the query called by the controller in a variable

  public ActionResult Index()
    {
        Session["dateDebut"] = DateTime.Now.AddMonths(-1).ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
        Session["dateFin"] = DateTime.Now.AddDays(0).ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
        HostClassReq hostClassChart = new HostClassReq();
        Chart_Event cex = new Chart_Event();

        var viewModel = new Chart_Event
        {
            chartVM = hostClassChart.callHostClass()
        };

        return View(viewModel);
    }

Here is the methode callHostClass implementation

    public Highcharts callHostClass()
    {
        DateTime begin = DateTime.ParseExact(HttpContext.Current.Session["dateDebut"].ToString(), "dd/MM/yyyy",
                                     System.Globalization.CultureInfo.InvariantCulture);
        DateTime end = DateTime.ParseExact(HttpContext.Current.Session["dateFin"].ToString(), "dd/MM/yyyy",
                                   System.Globalization.CultureInfo.InvariantCulture).AddDays(1);
        CreateChart createChart = new CreateChart();
        List<String> xs = new List<string>();





        var maListe = (from p in db.exclure
                      where (p.type.Contains("Host Class"))
                      group p by p.libelle into g
                      select new
                      {
                          libellex = g.Key
                      }).ToList();

        List<string> strListe = new List<string>();

        foreach (var x in maListe.Select(i => i.libellex))
        {

            strListe.Add(x.ToString());
        }



           var   myList = (from p in db.Full
                      where ( (p.date_reception > begin & p.date_reception < end & !p.mc_host_class.Contains("NULL")) &

                   (!strListe.Contains(p.mc_host_class)))
                      group p by p.mc_host_class into g
                      orderby g.Count() descending
                      select new
                      {
                          hostclassx = g.Key,
                          countx = g.Count()
                      }).Take(10).ToList();



       //  HttpContext.Current.Session["allList"] = myList;
         List<Full> questions = (List<Full>)HttpContext.Current.Session["allList"];

       //  questions = List <Full> myList;
         foreach (var x in questions)
         {

         }



         object[] ys =  myList.Select(a => (object)a.countx.ToString()).ToArray();






        foreach (var x in myList.Select(i => i.hostclassx))
        {
            if (x.Length > 20)
            {
                xs.Add((x.Substring(0, 20)));
            }
            else
            {
                xs.Add(x);
            }

        }



        var chart = createChart.createChartBar(xs, ys, 10);

        return chart;


    }

I need to store the result of myList query in a variable that will be accessed by another classes i need some help.

1
0
3/2/2015 10:40:15 AM

Accepted Answer

Are you looking for this?

Session["yourName"]=myList;

EDIT After question edit emerged that he wanted to use session in a class not extending Controller.

NEW PART

Therefore you can't use the initial suggestion but instead include System.Web using System.Web; and use

HttpContext.Current.Session["yourName"]=myList;

When you have to get it you use

var yourList = (myListType)Session["yourName"];

if you are in a class extending Controller or

var yourList = (myListType)HttpContext.Current.Session["yourName"];

otherwise.

2
3/2/2015 11:08:31 AM

Popular Answer

Try this:

HttpContext.Current.Session["name"] = mylist;

But be careful accessing session like that, might cause null-ref exceptions



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow