0

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.

@model Tuple<Namespace.Models.Class1[], Namespace.Models.Class2[]>
....
         @{
    int i, x = 0;
    int[] arr1;
    int[] arr2;
    }
    @foreach (var item in Model.Item1)
    {            

        arr1[i] = item.count;
        <tr>
            <td>@item.year</td>
            <td>@item.month</td>
            <td>@item.count</td>
        </tr>
        i++;
    }
....
   @foreach (var item in Model.Item2)
    {                        
        arr2[x] = item.count;
        <tr>
            <td>@item.year</td>
            <td>@item.month</td>
            <td>@item.count</td>
        </tr>
        x++;
    }
....
    @{        
        int[] total = Namespace.Models.GetTotal.ArrTotal(arr1, arr2);        
    }
    @foreach (var t in total)
    {
    <tr>
        <td>@t</td>
    </tr>
    }

Edit- some more information: This is the controller where I am running two queries:

var start = DateTime.Today.AddMonths(-15);
var query1 = (from t in db.table1
                     where t.ExportTemplate == "template1" && t.LogDate >= 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 => s.ObjectGUID).Distinct().Count()
                     }
                    );

        var query2 = (from t in db2.table2
                       where t.UserID == 3 && t.RequestID == 3 && t.ErrorID == 0 && t.LogDate >= 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 => s.GID).Distinct().Count()
                       }
                    );
        return View(System.Tuple.Create(query1.ToArray(), query2.ToArray()));

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

6
  • That should be throwing an error. You can't just declare an empty array and start assigning values to indices that don't exist. I don't know why it's timing out instead of giving you an error. Commented May 7, 2013 at 11:41
  • well I can not have more then 15 indices of each array, I have tried declaring an array with 15 slots like int[] arr1 = new int[15]; Commented May 7, 2013 at 11:43
  • OK... Where is it timing out? What is the line of code where it says it is timing out? Commented May 7, 2013 at 11:47
  • Apart from the fact that your problem seems very strange, why are you even doing this? This is a view, you shouldn't be putting code in here (that's not related to rendering). If you want the total, just add it to your Model (or create a ViewModel if you don't want to pollute your Model) Commented May 7, 2013 at 11:54
  • I am trying to figure out how to combine using tuple with two models and put this someplace in the model as well. The important thing I want to avoid is to not run the databas query several times because I am already running queries to two different databases. Commented May 7, 2013 at 12:02

1 Answer 1

1

When you call ToArray it is evaluated in your controller. The database is hit there. However are you using the entity framework? Then you are still bound to the context and maybe lazy loading and all that. Make sure you detach the objects.

Could be the total method you use may have an issue too.

Sign up to request clarification or add additional context in comments.

1 Comment

Yepp I'm using entity framework. Thnx for the tip, I'll look into it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.