user2609980
user2609980

Reputation: 10474

Foreach loop not displaying first entry

In our MVC4 application my database has an Orders table, a Products table and a Category table. Every Order has a product (plus quantity) in a foreign key relationship and the product belongs to a category with a foreign key.

So Order has an OrderId, ProductId (FK to Product), a data plus a quantity and Product has a ProductId, Name, and a CategoryId (FK to Category) and Category has a CategoryId and a Name.

Now we made a "shopping list" which displays the categories that are ordered during the day and display the products below the category name.

To only display the products of this date we are using the foreach loop below:

@model Tuple<List<myproject.Models.Order>, List<myproject.Models.Order>>
@foreach (var item1 in Model.Item1)
    {
        if (item1.Date.ToString("d").Equals(DateTime.UtcNow.Date.ToString("d")))
        {
            <tr>    
                <td>
                    <h3>@Html.DisplayFor(catName => item1.Product.Category.Name)</h3>
                </td>
            </tr>
        }

        foreach (var item2 in Model.Item2)
        {
            if (item2.Product.Category.Name.Equals(item1.Product.Category.Name) &&
                 item2.Date.ToString("d").Equals(DateTime.UtcNow.Date.ToString("d")))
            {

                <tr>
                    <td>

                            @Html.DisplayFor(productName => item2.Product.Name)
                            &nbsp(x @Html.DisplayFor(quantity => item2.Quantity))

                    </td>
                </tr>

            }
        }
    }

to display categories and the products in the category it misses the first name of the category in the list. The output is:

Apple   (x 1)  
Banana  (x 1)  

Snacks
Fries   (x 3)  
Hamburger(x 1)  

Veggies
Tomato   (x 1)  

instead of

Fruit 
Apple   (x 1)  
Banana  (x 1)  

Snacks
Fries   (x 3)  
Hamburger(x 1)  

Veggies
Tomato   (x 1)  

I've been breaking my head on this for the past hour. Can someone explain why it is not displaying the entry for "fruit"?

Upvotes: 0

Views: 171

Answers (2)

James
James

Reputation: 82096

My guess is this is a daylight savings issue - you will probably find your UTC date has been adjusted such that the date changes when represented as UTC. For example, in the UK if I created a record at 01/04/2013 23:00:00 local time, in UTC time that's represented as 02/04/2013 00:00:00 because we are observing DST during this time (+1 hour).

To avoid these types of problems you should convert the UTC back to local time and compare using DateTime.Now.Date.

Upvotes: 1

p.s.w.g
p.s.w.g

Reputation: 149000

You've surrounded it with an if-block. It's not clear from the question why this is here or what you're trying to accomplish with it. Most likely there's a logical flaw in how you're trying to filter the categories, or in how you're storing the relevant data. In any case, if you remove that, you should see the category names as you expect.

@foreach (var item1 in Model.Item1)
{
    <tr>    
        <td>
            <h3>@Html.DisplayFor(catName => item1.Product.Category.Name)</h3>
        </td>
    </tr>

    foreach (var item2 in Model.Item2)
    {
        ...
    }
}

Upvotes: 1

Related Questions