title: How to build array inside foreach loop in razor view
body: <p>I am using MVC4 and C# and I'm trying to build and array in the view while I am looping through my model, for some reason I am getting a "Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding." It works fine if I remove the part where I try to build the arrays arr1 and arr2 and send them to the method called ArrTotal. This seems like a no brainer, no idea why it would cause a timeout. it should be building the arrays during the same query as before, and all that the method ArrTotal does is to add values from the two arrays together, no new query should be taking place to cause the server to timeout.</p>
  
  <pre><code>@model Tuple&lt;Namespace.Models.Class1[], Namespace.Models.Class2[]&gt;
  ....
           @{
      int i, x = 0;
      int[] arr1;
      int[] arr2;
      }
      @foreach (var item in Model.Item1)
      {            
  
          arr1[i] = item.count;
          &lt;tr&gt;
              &lt;td&gt;@item.year&lt;/td&gt;
              &lt;td&gt;@item.month&lt;/td&gt;
              &lt;td&gt;@item.count&lt;/td&gt;
          &lt;/tr&gt;
          i++;
      }
  ....
     @foreach (var item in Model.Item2)
      {                        
          arr2[x] = item.count;
          &lt;tr&gt;
              &lt;td&gt;@item.year&lt;/td&gt;
              &lt;td&gt;@item.month&lt;/td&gt;
              &lt;td&gt;@item.count&lt;/td&gt;
          &lt;/tr&gt;
          x++;
      }
  ....
      @{        
          int[] total = Namespace.Models.GetTotal.ArrTotal(arr1, arr2);        
      }
      @foreach (var t in total)
      {
      &lt;tr&gt;
          &lt;td&gt;@t&lt;/td&gt;
      &lt;/tr&gt;
      }
  </code></pre>
  
  <p>Edit- some more information:
  This is the controller where I am running two queries:</p>
  
  <pre><code>var start = DateTime.Today.AddMonths(-15);
  var query1 = (from t in db.table1
                       where t.ExportTemplate == "template1" &amp;&amp; t.LogDate &gt;= start
                       group t by new { Year = t.LogDate.Year, Month = t.LogDate.Month } into tg
                       orderby tg.Key.Year, tg.Key.Month
                       select new Class1
                       {
                           year = tg.Key.Year,
                           month = tg.Key.Month,
                           count = tg.Select(s =&gt; s.ObjectGUID).Distinct().Count()
                       }
                      );
  
          var query2 = (from t in db2.table2
                         where t.UserID == 3 &amp;&amp; t.RequestID == 3 &amp;&amp; t.ErrorID == 0 &amp;&amp; t.LogDate &gt;= start
                         group t by new { Year = t.LogDate.Value.Year, Month = t.LogDate.Value.Month } into tg
                         orderby tg.Key.Year, tg.Key.Month
                         select new Class2
                         {
                             year = tg.Key.Year,
                             month = tg.Key.Month,
                             count = tg.Select(s =&gt; s.GID).Distinct().Count()
                         }
                      );
          return View(System.Tuple.Create(query1.ToArray(), query2.ToArray()));
  </code></pre>
  
  <p>its the return View that times out.
  Like I said, it works as long as I dont try to build the arrays from the view</p>