Speichern Sie die Liste der Objekte in Session-variable

Will ich speichern das Ergebnis der Abfrage genannt, die von der Steuerung in eine 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);
    }

Hier ist die methode callHostClass Umsetzung

    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;


    }

Muss ich speichern das Ergebnis myList Abfrage in einer Variablen zugegriffen wird, die von anderen Klassen brauche ich etwas Hilfe.

  • .ToList().Take(10); produzieren kann einige performance-Probleme. Holen Sie sich alle Entitäten in den Speicher und dann nehmen Sie die 10 ersten. Sie könnten die ersten 10 auf der db-Seite und dann Holen Sie es in den Speicher mit ToList().
  • Bitte, wie es zu tun, ich bin neu in der asp mvc
  • Sie müssen, um Haupt-Abfrage und ersetzen Sie dann ToList mit Take: .Take(10).ToList(). Die Bestellung ist erforderlich, um Take oder Skip.
  • das ist hilfreich, danke.
InformationsquelleAutor | 2015-03-02
Schreibe einen Kommentar