title: MY Asp.Net MVC page takes 12 secs. to load
body: <p>I have an MVC Controller like this</p>
  
  <pre><code>public ActionResult Index(int vendor=-1, int product = -1, string error="", string mode="Shortfall")
      {
          if (Session["UserId"] == null)
              return RedirectToAction("Index", "Login");
          var products = DbContext.GetAllProducts();
          List&lt;SurplusViewModel&gt; surplusList = new List&lt;SurplusViewModel&gt;();
          Dictionary&lt;int, string&gt; searchVendor = new Dictionary&lt;int, string&gt;();
          Dictionary&lt;int, string&gt; searchProds = new Dictionary&lt;int, string&gt;();
          if (products.Count() &gt; 0)
          {
              foreach (var prod in products)
              {
                  SurplusViewModel s = new SurplusViewModel(prod);
                  surplusList.Add(s);
                  foreach (var v in s.Vendors)
                  {
                      if (!searchVendor.ContainsKey(v.CorpId))
                      {
                          searchVendor.Add(v.CorpId, v.CorpName);
                      }
                  }
                  if(!searchProds.ContainsKey(s.ProductId))
                      searchProds.Add(s.ProductId, s.ProductVM.ProductCode + " / " + s.ProductVM.ProductPartNo);
              }
          }
          ViewData["vendorList"] = searchVendor;
          ViewData["productList"] = searchProds;
          ViewData["selectVendor"] = vendor;
          ViewData["selectProd"] = product;
          ViewData["mode"] = mode;
          ViewBag.Message = "";
          ViewBag.Error = "";
          IEnumerable&lt;SurplusViewModel&gt; finalList = surplusList.OrderBy(o =&gt; o.Difference).ToList();
          if (vendor &gt; 0)
          {
              Corporation searchcorp = DbContext.GetCorporation(vendor);
              finalList = finalList.Where(x =&gt; x.VendorNames.IndexOf(searchcorp.CorpName) &gt;= 0);
          }
          if (product &gt; 0)
          {
              finalList = finalList.Where(x =&gt; x.ProductId == product);
          }
          if (vendor &lt; 0 &amp;&amp; product &lt; 0)
          {
              if (mode.Equals("Shortfall"))
                  finalList = finalList.Where(f =&gt; f.VendorQuantity - (f.CMQuantity + f.OEMQuantity) &lt; 0);
              else if (mode.Equals("Surplus"))
                  finalList = finalList.Where(f =&gt; f.VendorQuantity - (f.CMQuantity + f.OEMQuantity) &gt; 0);
  
  
          }
          return View(finalList);
          //return View();
      }
  </code></pre>
  
  <p>This takes about 20 seconds to load on localhost. What can I do to improve my app's loading time. If it takes 20 secs on localhost I assume it will be very slow on the internet. Any suggestions?</p>
  
  <p>EDIT: Code for SurplusViewModel</p>
  
  <pre><code>public SurplusViewModel(Product product)
      {
          int productId = product.ProductId;
          ProductId = productId;
          ProductVM = new ProductViewModel(product);
          var saleDetsCM = from s in DbContext.GetSalesOrderDetailsFromCM()
                           where s.ProductId == productId &amp;&amp; s.SaleStatus.Equals("Open") &amp;&amp; (s.OrderType.ToLower().Equals("prototype") || s.OrderType.ToLower().Equals("production"))
                           orderby s.SalDetId descending
                           select s;
  
          var saleDetsOEM = from s in DbContext.GetSalesOrderDetailsFromOEMs()
                            where s.ProductId == productId &amp;&amp; s.SaleStatus.Equals("Open") &amp;&amp; (s.OrderType.ToLower().Equals("prototype") || s.OrderType.ToLower().Equals("production"))
                            orderby s.SalDetId descending
                            select s;
  
          var shipQty = from s in DbContext.GetAllSalesDets()
                        where s.ProductId == productId &amp;&amp; !s.SaleStatus.Equals("Open") &amp;&amp; (s.OrderType.ToLower().Equals("prototype") || s.OrderType.ToLower().Equals("production"))
                        orderby s.SalDetId descending
                        select s;
  
          CustomerOrdersFromCMs = saleDetsCM.ToList();
          CustomerOrdersFromOEMs = saleDetsOEM.ToList();
          VendorOrders = (from p in DbContext.GetPurchaseDetsForProduct(productId)
                          where p.OrderType != null &amp;&amp; (p.OrderType.ToLower().Equals("prototype") || p.OrderType.ToLower().Equals("production"))
                          select p).ToList();
          var poIds = from v in VendorOrders
                       select v.PodPOId;
          BatchPurchaseDetails = DbContext.GetBatchPurchaseForProduct(productId).ToList();
          VendorOrderCount = 0;
          VendorQuantity = 0;
          var purchaseOrds = (from po in DbContext.GetPurchaseOrdersForProduct(productId)
                              where poIds.Contains(po.POId)
                              select po).ToList();
          List&lt;int&gt; vendIds = new List&lt;int&gt;();
          foreach (var po in purchaseOrds)
          {
              vendIds.Add(po.VendorId.Value);
          }
          var vendors = from v in DbContext.GetAllCorps()
                        where vendIds.Contains(v.CorpId)
                        select v;
  
          foreach (var podet in VendorOrders)
          {
              double totalbatchqty = 0;
              var purdetBatch = DbContext.GetBatchDetailsForPurchaseDet(podet.PodId);
              VendorQuantity += podet.Quantity;
              foreach (var b in purdetBatch)
              {
                  totalbatchqty += b.Quantity;
                  VendorQuantity -= b.Quantity;
              }
              if (totalbatchqty &gt;= podet.Quantity)
              {
  
              }
              else
              {
                  VendorOrderCount++;
              }
  
          }
  
          Vendors = vendors.ToList();
          VendorNames = "";
          foreach (var vnd in Vendors)
          {
              VendorNames += vnd.CorpName + ",";
          }
          if (VendorNames.Length &gt; 0)
          {
              VendorNames = VendorNames.Substring(0, VendorNames.Length - 1);
          }
  
  
          OEMQuantity = 0;
          foreach (var item in CustomerOrdersFromOEMs)
          {
              OEMQuantity += item.Quantity;
          }
  
          CMQuantity = 0;
          foreach (var item in CustomerOrdersFromCMs)
          {
              CMQuantity += item.Quantity;
          }
          ShipQuantity = 0;
          foreach (var item in shipQty)
          {
              ShipQuantity += item.Quantity;
          }
          Difference = VendorQuantity - (CMQuantity + OEMQuantity);
          //TotalInsideSalesOrder = VendorOrders.Count();
  
      }
  </code></pre>