title: Caching a list in ASP.NET MVC 5
body: <p>I have a thousands of users in my MVC APP and I use sort, paging and searching in my table of users (<a href="http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application" rel="nofollow">by this tutorial</a>), but it takes a too long to load, sort or search.</p>
  
  <p>So I have list of users </p>
  
  <pre><code>List&lt;ApplicationUser&gt; appUsers = _context.Users.ToList();
  </code></pre>
  
  <p>and I have list of models</p>
  
  <pre><code>List&lt;RegisterViewModel&gt; models = new List&lt;RegisterViewModel&gt;();
  foreach (var au in appUsers)
  {
      RegisterViewModel rvm = new RegisterViewModel(au);
      rvm.RoleName = UserManager.GetRoles(au.Id).First();
      models.Add(rvm); //new RegisterViewModel(au));
  }
  </code></pre>
  
  <p>And in models I search, or sort.</p>
  
  <p>What is the simplest way to cache this list of models and how can I use cached list instead of creating new list?</p>
  
  <p>Here is my complete code of <code>ActionResult Index()</code></p>
  
  <pre><code>[OutputCache(Duration = 120, VaryByParam = "*")]
  [AuthLog(Roles = "SuperAdmin")]
  public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
  {
      ViewBag.CurrentSort = sortOrder;
      ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name_desc" : "";
      ViewBag.EmailSortParm = sortOrder == "Email" ? "Email_desc" : "Email";
  
      if (searchString != null)
          page = 1;
      else
          searchString = currentFilter;
  
      ViewBag.CurrentFilter = searchString;
  
      List&lt;ApplicationUser&gt; appUsers = _context.Users.ToList();
  
      List&lt;RegisterViewModel&gt; models = new List&lt;RegisterViewModel&gt;();
      foreach (var au in appUsers)
      {
          RegisterViewModel rvm = new RegisterViewModel(au);
          rvm.RoleName = UserManager.GetRoles(au.Id).First();
          models.Add(rvm); //new RegisterViewModel(au));
      }
  
      if (!String.IsNullOrEmpty(searchString))
          models = models.Where(s =&gt; s.Name.ToUpper().Contains(searchString.ToUpper()) || s.Email.ToUpper().Contains(searchString.ToUpper())).ToList();
  
      switch (sortOrder)
      {
          case "Name_desc":
              models = models.OrderByDescending(x =&gt; x.Name).ToList();
              break;
          case "Email_desc":
              models = models.OrderByDescending(x =&gt; x.Email).ToList();
              break;
          case "Email":
              models = models.OrderBy(x =&gt; x.Email).ToList();
              break;
          default:
              models = models.OrderBy(x =&gt; x.Name).ToList();
              break;
      }
      int pageSize = 20;
      int pageNumber = (page ?? 1);
  
      ViewBag.Title = "Použivatelia";
      return View(models.AsEnumerable().ToPagedList(pageNumber, pageSize));
  }
  </code></pre>