0

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"?

user2609980
  • 10,264
  • 15
  • 74
  • 143

2 Answers2

1

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)
    {
        ...
    }
}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • Well I only want the categories that are ordered today for printing the list of products (it's for an application where people can order food by day and then one person picks it up with this generated shopping list). So the upper if-block is necessary, but not working. – user2609980 Nov 11 '13 at 13:02
  • 1
    @user2609980 Honestly, it would probably be better to try to filter the result set in your controller when you query it from the database. If there is a logical flaw, that will probably also make it easier to figure out where the it is. – p.s.w.g Nov 11 '13 at 13:06
  • This was indeed the solution. Today I looked at it again and solved the issue by updating the controller to only bring the current date (currentdate is DateTime.Now.Date) to the model as follows: var orders = db.Orders.Include(o => o.Product) .Where(o => o.Date == currentDate) .GroupBy(o => o.Product.Category.Name) .Select(cat => cat.FirstOrDefault()); Stil don't know why the if-statement that tried to accomplish the same thing (only current date) excluded the first entry, but now it works. Thanks for your help. – user2609980 Nov 19 '13 at 14:16
  • @user2609980 glad I could help, even if indirectly. – p.s.w.g Nov 19 '13 at 14:29
1

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.

James
  • 80,725
  • 18
  • 167
  • 237
  • Well this sounds plausible but changing to DateTime.Now.Date gives the same output. – user2609980 Nov 11 '13 at 13:03
  • 1
    @user2609980 you are still comparing a UTC date to a local date, you need to convert `item1.Date` back to local time (for which you need timezone information). – James Nov 11 '13 at 13:05
  • Well the dates are added at 6/11/2013 and today and all in the middle of the day so I suppose this is not the issue. – user2609980 Nov 11 '13 at 13:08
  • 1
    @user2609980 what is the full value of `item1.Date` and what timezone are you running on? – James Nov 11 '13 at 13:09
  • It is for example 06/11/2013 14:44:38. It comes from a tuple which contains the query (item1) var orders = db.Orders.Include(o => o.Product) .GroupBy(o => o.Product.Category.Name) .Select(cat => cat.FirstOrDefault()); I'll add more info on the query and models. Timezone I'm running on is UTC+1. – user2609980 Nov 11 '13 at 13:14
  • @user2609980 there's your problem - `06/11/2013` is not equal to `11/11/2013`. What exactly is the reasoning behind the date check? It doesn't make sense to hide only the caption if the dates don't match anyway. On top of that, any sort of filtering should really be done at the server end which keeps your UI light. Also, if it is a date-dependant check you should be working with the actual date and not converting to strings... – James Nov 11 '13 at 13:24
  • No that is unfortunately not the problem, this was just an example. It is displaying the products of 11/11/2013 on different times. The banana and apple are of that date as well. Only the Category Name (Fruit) of the same date is missing. – user2609980 Nov 11 '13 at 13:27
  • 1
    @user2609980 can you provide the *actual* dates then? Obviously there is an issue here, also just to confirm `item.Date` is UTC and not local? – James Nov 11 '13 at 13:32
  • Well I accidentally emptied the database and now (with new products) it is working fine. Thank you for your suggestions! – user2609980 Nov 11 '13 at 14:06
  • @user2609980 that, to me, should ring alarm bells. – James Nov 11 '13 at 14:43
  • Yes alarm bells should have gone off since the next day the problem reappeared. Now it is solved with the idea of p.s.w.g. above (placing the logic to only get today's date in the controller). Still don't understand why the if-statement included a first empty value for category. At least it works now. Thanks again. – user2609980 Nov 19 '13 at 14:21