存储Session变量中的对象列表

时间:2015-03-02 09:43:26

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

我想将控制器调用的查询结果存储在变量

  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);
    }

这是方法callHostClass实现

    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;


    }

我需要将myList查询的结果存储在一个变量中,该变量将由我需要帮助的其他类访问。​​

2 个答案:

答案 0 :(得分:2)

你在找这个吗?

Session["yourName"]=myList;

编辑问题编辑出现后,他想在不扩展Controller的类中使用会话。

NEW PART

因此,您无法使用初始建议,而是包含System.Web using System.Web;并使用

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

当你必须得到它时,你可以使用

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

如果您在扩展Controller或

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

否则。

答案 1 :(得分:1)

试试这个:

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

但是要小心访问这样的会话,可能会导致null-ref异常